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 spin_lock(&scrub->os_lock);
399 scrub->os_waiting = 0;
400 scrub->os_paused = 0;
401 spin_unlock(&scrub->os_lock);
402 scrub->os_new_checked = 0;
403 if (sf->sf_pos_last_checkpoint != 0)
404 sf->sf_pos_latest_start = sf->sf_pos_last_checkpoint + 1;
406 sf->sf_pos_latest_start = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
408 scrub->os_pos_current = sf->sf_pos_latest_start;
409 sf->sf_status = SS_SCANNING;
410 sf->sf_time_latest_start = cfs_time_current_sec();
411 sf->sf_time_last_checkpoint = sf->sf_time_latest_start;
412 rc = osd_scrub_file_store(scrub);
414 spin_lock(&scrub->os_lock);
415 thread_set_flags(thread, SVC_RUNNING);
416 spin_unlock(&scrub->os_lock);
417 wake_up_all(&thread->t_ctl_waitq);
419 up_write(&scrub->os_rwsem);
425 osd_scrub_convert_ff(struct osd_thread_info *info, struct osd_device *dev,
426 struct inode *inode, const struct lu_fid *fid)
428 struct filter_fid_old *ff = &info->oti_ff;
429 struct dentry *dentry = &info->oti_obj_dentry;
433 bool removed = false;
437 /* We want the LMA to fit into the 256-byte OST inode, so operate
439 * 1) read old XATTR_NAME_FID and save the parent FID;
440 * 2) delete the old XATTR_NAME_FID;
441 * 3) make new LMA and add it;
442 * 4) generate new XATTR_NAME_FID with the saved parent FID and add it.
444 * Making the LMA to fit into the 256-byte OST inode can save time for
445 * normal osd_check_lma() and for other OI scrub scanning in future.
446 * So it is worth to make some slow conversion here. */
447 jh = ldiskfs_journal_start_sb(osd_sb(dev),
448 osd_dto_credits_noquota[DTO_XATTR_SET] * 3);
451 CERROR("%s: fail to start trans for convert ff: "DFID
453 osd_name(dev), PFID(fid), rc);
457 /* 1) read old XATTR_NAME_FID and save the parent FID */
458 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
459 if (rc == sizeof(*ff)) {
460 /* 2) delete the old XATTR_NAME_FID */
461 ll_vfs_dq_init(inode);
462 rc = inode->i_op->removexattr(dentry, XATTR_NAME_FID);
467 } else if (unlikely(rc == -ENODATA)) {
469 } else if (rc != sizeof(struct filter_fid)) {
470 GOTO(stop, rc = -EINVAL);
473 /* 3) make new LMA and add it */
474 rc = osd_ea_fid_set(info, inode, fid, LMAC_FID_ON_OST, 0);
475 if (rc == 0 && reset)
476 size = sizeof(struct filter_fid);
477 else if (rc != 0 && removed)
478 /* If failed, we should try to add the old back. */
479 size = sizeof(struct filter_fid_old);
481 /* 4) generate new XATTR_NAME_FID with the saved parent FID and add it*/
485 rc1 = __osd_xattr_set(info, inode, XATTR_NAME_FID, ff, size,
487 if (rc1 != 0 && rc == 0)
494 ldiskfs_journal_stop(jh);
499 osd_scrub_check_update(struct osd_thread_info *info, struct osd_device *dev,
500 struct osd_idmap_cache *oic, int val)
502 struct osd_scrub *scrub = &dev->od_scrub;
503 struct scrub_file *sf = &scrub->os_file;
504 struct lu_fid *fid = &oic->oic_fid;
505 struct osd_inode_id *lid = &oic->oic_lid;
506 struct osd_inode_id *lid2 = &info->oti_id;
507 struct osd_inconsistent_item *oii = NULL;
508 struct inode *inode = NULL;
509 int ops = DTO_INDEX_UPDATE;
512 bool converted = false;
515 down_write(&scrub->os_rwsem);
516 scrub->os_new_checked++;
520 if (scrub->os_in_prior)
521 oii = cfs_list_entry(oic, struct osd_inconsistent_item,
524 if (lid->oii_ino < sf->sf_pos_latest_start && oii == NULL)
527 if (fid_is_igif(fid))
530 if (val == SCRUB_NEXT_OSTOBJ_OLD) {
531 inode = osd_iget(info, dev, lid);
534 /* Someone removed the inode. */
535 if (rc == -ENOENT || rc == -ESTALE)
540 sf->sf_flags |= SF_UPGRADE;
541 sf->sf_internal_flags &= ~SIF_NO_HANDLE_OLD_FID;
542 dev->od_check_ff = 1;
543 rc = osd_scrub_convert_ff(info, dev, inode, fid);
544 rc = osd_ea_fid_set(info, inode, fid,
552 if ((val == SCRUB_NEXT_NOLMA) &&
553 (!scrub->os_convert_igif || OBD_FAIL_CHECK(OBD_FAIL_FID_NOLMA)))
556 if ((oii != NULL && oii->oii_insert) || (val == SCRUB_NEXT_NOLMA))
559 rc = osd_oi_lookup(info, dev, fid, lid2,
560 (val == SCRUB_NEXT_OSTOBJ ||
561 val == SCRUB_NEXT_OSTOBJ_OLD) ? OI_KNOWN_ON_OST : 0);
568 inode = osd_iget(info, dev, lid);
571 /* Someone removed the inode. */
572 if (rc == -ENOENT || rc == -ESTALE)
578 scrub->os_full_speed = 1;
579 ops = DTO_INDEX_INSERT;
580 idx = osd_oi_fid2idx(dev, fid);
582 case SCRUB_NEXT_NOLMA:
583 sf->sf_flags |= SF_UPGRADE;
584 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
588 if (!(sf->sf_flags & SF_INCONSISTENT))
589 dev->od_igif_inoi = 0;
591 case SCRUB_NEXT_OSTOBJ:
592 sf->sf_flags |= SF_INCONSISTENT;
593 case SCRUB_NEXT_OSTOBJ_OLD:
596 sf->sf_flags |= SF_RECREATED;
597 if (unlikely(!ldiskfs_test_bit(idx, sf->sf_oi_bitmap)))
598 ldiskfs_set_bit(idx, sf->sf_oi_bitmap);
601 } else if (osd_id_eq(lid, lid2)) {
603 sf->sf_items_updated++;
607 scrub->os_full_speed = 1;
608 sf->sf_flags |= SF_INCONSISTENT;
610 /* XXX: If the device is restored from file-level backup, then
611 * some IGIFs may have been already in OI files, and some
612 * may be not yet. Means upgrading from 1.8 may be partly
613 * processed, but some clients may hold some immobilized
614 * IGIFs, and use them to access related objects. Under
615 * such case, OSD does not know whether an given IGIF has
616 * been processed or to be processed, and it also cannot
617 * generate local ino#/gen# directly from the immobilized
618 * IGIF because of the backup/restore. Then force OSD to
619 * lookup the given IGIF in OI files, and if no entry,
620 * then ask the client to retry after upgrading completed.
621 * No better choice. */
622 dev->od_igif_inoi = 1;
625 rc = osd_scrub_refresh_mapping(info, dev, fid, lid, ops,
626 (val == SCRUB_NEXT_OSTOBJ ||
627 val == SCRUB_NEXT_OSTOBJ_OLD) ? OI_KNOWN_ON_OST : 0);
629 if (scrub->os_in_prior)
630 sf->sf_items_updated_prior++;
632 sf->sf_items_updated++;
634 /* The target has been changed, need to be re-loaded. */
635 lu_object_purge(info->oti_env, osd2lu_dev(dev), fid);
642 sf->sf_items_failed++;
643 if (sf->sf_pos_first_inconsistent == 0 ||
644 sf->sf_pos_first_inconsistent > lid->oii_ino)
645 sf->sf_pos_first_inconsistent = lid->oii_ino;
650 /* There may be conflict unlink during the OI scrub,
651 * if happend, then remove the new added OI mapping. */
652 if (ops == DTO_INDEX_INSERT && inode != NULL && !IS_ERR(inode) &&
653 unlikely(inode->i_nlink == 0))
654 osd_scrub_refresh_mapping(info, dev, fid, lid,
656 (val == SCRUB_NEXT_OSTOBJ ||
657 val == SCRUB_NEXT_OSTOBJ_OLD) ?
658 OI_KNOWN_ON_OST : 0);
659 up_write(&scrub->os_rwsem);
661 if (inode != NULL && !IS_ERR(inode))
665 LASSERT(!cfs_list_empty(&oii->oii_list));
667 spin_lock(&scrub->os_lock);
668 cfs_list_del_init(&oii->oii_list);
669 spin_unlock(&scrub->os_lock);
672 RETURN(sf->sf_param & SP_FAILOUT ? rc : 0);
675 static int osd_scrub_checkpoint(struct osd_scrub *scrub)
677 struct scrub_file *sf = &scrub->os_file;
680 if (likely(cfs_time_before(cfs_time_current(),
681 scrub->os_time_next_checkpoint) ||
682 scrub->os_new_checked == 0))
685 down_write(&scrub->os_rwsem);
686 sf->sf_items_checked += scrub->os_new_checked;
687 scrub->os_new_checked = 0;
688 sf->sf_pos_last_checkpoint = scrub->os_pos_current;
689 sf->sf_time_last_checkpoint = cfs_time_current_sec();
690 sf->sf_run_time += cfs_duration_sec(cfs_time_current() + HALF_SEC -
691 scrub->os_time_last_checkpoint);
692 rc = osd_scrub_file_store(scrub);
693 up_write(&scrub->os_rwsem);
698 static void osd_scrub_post(struct osd_scrub *scrub, int result)
700 struct scrub_file *sf = &scrub->os_file;
703 down_write(&scrub->os_rwsem);
704 spin_lock(&scrub->os_lock);
705 thread_set_flags(&scrub->os_thread, SVC_STOPPING);
706 spin_unlock(&scrub->os_lock);
707 if (scrub->os_new_checked > 0) {
708 sf->sf_items_checked += scrub->os_new_checked;
709 scrub->os_new_checked = 0;
710 sf->sf_pos_last_checkpoint = scrub->os_pos_current;
712 sf->sf_time_last_checkpoint = cfs_time_current_sec();
714 struct osd_device *dev =
715 container_of0(scrub, struct osd_device, od_scrub);
717 dev->od_igif_inoi = 1;
718 dev->od_check_ff = 0;
719 sf->sf_status = SS_COMPLETED;
720 memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
721 sf->sf_flags &= ~(SF_RECREATED | SF_INCONSISTENT |
722 SF_UPGRADE | SF_AUTO);
723 sf->sf_time_last_complete = sf->sf_time_last_checkpoint;
724 sf->sf_success_count++;
725 } else if (result == 0) {
726 if (scrub->os_paused)
727 sf->sf_status = SS_PAUSED;
729 sf->sf_status = SS_STOPPED;
731 sf->sf_status = SS_FAILED;
733 sf->sf_run_time += cfs_duration_sec(cfs_time_current() + HALF_SEC -
734 scrub->os_time_last_checkpoint);
735 result = osd_scrub_file_store(scrub);
737 CERROR("%.16s: fail to osd_scrub_post, rc = %d\n",
738 LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,
740 up_write(&scrub->os_rwsem);
745 /* iteration engine */
747 struct osd_iit_param {
748 struct super_block *sb;
749 struct buffer_head *bitmap;
755 typedef int (*osd_iit_next_policy)(struct osd_thread_info *info,
756 struct osd_device *dev,
757 struct osd_iit_param *param,
758 struct osd_idmap_cache **oic,
761 typedef int (*osd_iit_exec_policy)(struct osd_thread_info *info,
762 struct osd_device *dev,
763 struct osd_iit_param *param,
764 struct osd_idmap_cache *oic,
765 int *noslot, int rc);
767 static int osd_iit_next(struct osd_iit_param *param, __u32 *pos)
769 param->offset = ldiskfs_find_next_bit(param->bitmap->b_data,
770 LDISKFS_INODES_PER_GROUP(param->sb), param->offset);
771 if (param->offset >= LDISKFS_INODES_PER_GROUP(param->sb)) {
772 *pos = 1 + (param->bg+1) * LDISKFS_INODES_PER_GROUP(param->sb);
773 return SCRUB_NEXT_BREAK;
775 *pos = param->gbase + param->offset;
781 * \retval SCRUB_NEXT_OSTOBJ_OLD: FID-on-OST
782 * \retval 0: FID-on-MDT
784 static int osd_scrub_check_local_fldb(struct osd_thread_info *info,
785 struct osd_device *dev,
788 /* XXX: The initial OI scrub will scan the top level /O to generate
789 * a small local FLDB according to the <seq>. If the given FID
790 * is in the local FLDB, then it is FID-on-OST; otherwise it's
791 * quite possible for FID-on-MDT. */
795 static int osd_scrub_get_fid(struct osd_thread_info *info,
796 struct osd_device *dev, struct inode *inode,
797 struct lu_fid *fid, bool scrub)
799 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
801 bool has_lma = false;
803 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
806 if (lma->lma_compat & LMAC_NOT_IN_OI) {
807 ldiskfs_set_inode_state(inode,
808 LDISKFS_STATE_LUSTRE_NO_OI);
809 return SCRUB_NEXT_CONTINUE;
812 *fid = lma->lma_self_fid;
813 if (fid_is_internal(&lma->lma_self_fid)) {
815 rc = SCRUB_NEXT_CONTINUE;
822 if (fid_is_namespace_visible(fid) && !fid_is_norm(fid))
825 if (lma->lma_compat & LMAC_FID_ON_OST)
826 return SCRUB_NEXT_OSTOBJ;
828 if (fid_is_idif(fid) || fid_is_last_id(fid))
829 return SCRUB_NEXT_OSTOBJ_OLD;
831 if (lma->lma_incompat & LMAI_AGENT)
832 return SCRUB_NEXT_CONTINUE;
834 /* Here, it may be MDT-object, or may be 2.4 OST-object.
838 if (rc == -ENODATA || rc == 0) {
839 rc = osd_get_idif(info, inode, &info->oti_obj_dentry, fid);
842 /* It is old 2.x (x <= 3) or 1.8 OST-object. */
843 rc = SCRUB_NEXT_OSTOBJ_OLD;
849 /* It is FID-on-OST, but we do not know how
850 * to generate its FID, ignore it directly. */
851 rc = SCRUB_NEXT_CONTINUE;
853 /* It is 2.4 OST-object. */
854 rc = SCRUB_NEXT_OSTOBJ_OLD;
862 if (dev->od_scrub.os_convert_igif) {
863 lu_igif_build(fid, inode->i_ino,
864 inode->i_generation);
866 rc = SCRUB_NEXT_NOLMA;
870 /* It may be FID-on-OST, or may be FID for
871 * non-MDT0, anyway, we do not know how to
872 * generate its FID, ignore it directly. */
873 rc = SCRUB_NEXT_CONTINUE;
878 /* For OI scrub case only: the object has LMA but has no ff
879 * (or ff crashed). It may be MDT-object, may be OST-object
880 * with crashed ff. The last check is local FLDB. */
881 rc = osd_scrub_check_local_fldb(info, dev, fid);
887 static int osd_iit_iget(struct osd_thread_info *info, struct osd_device *dev,
888 struct lu_fid *fid, struct osd_inode_id *lid, __u32 pos,
889 struct super_block *sb, bool scrub)
895 osd_id_gen(lid, pos, OSD_OII_NOGEN);
896 inode = osd_iget(info, dev, lid);
899 /* The inode may be removed after bitmap searching, or the
900 * file is new created without inode initialized yet. */
901 if (rc == -ENOENT || rc == -ESTALE)
902 RETURN(SCRUB_NEXT_CONTINUE);
904 CERROR("%.16s: fail to read inode, ino# = %u, rc = %d\n",
905 LDISKFS_SB(sb)->s_es->s_volume_name, pos, rc);
909 /* If the inode has no OI mapping, then it is special locally used,
910 * should be invisible to OI scrub or up layer LFSCK. */
911 if (ldiskfs_test_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI))
912 GOTO(put, rc = SCRUB_NEXT_CONTINUE);
915 ldiskfs_test_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB)) {
916 /* Only skip it for the first OI scrub accessing. */
917 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
918 GOTO(put, rc = SCRUB_NEXT_NOSCRUB);
921 rc = osd_scrub_get_fid(info, dev, inode, fid, scrub);
930 static int osd_scrub_next(struct osd_thread_info *info, struct osd_device *dev,
931 struct osd_iit_param *param,
932 struct osd_idmap_cache **oic, int noslot)
934 struct osd_scrub *scrub = &dev->od_scrub;
935 struct ptlrpc_thread *thread = &scrub->os_thread;
937 struct osd_inode_id *lid;
940 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_DELAY) && cfs_fail_val > 0) {
941 struct l_wait_info lwi;
943 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val), NULL, NULL);
944 l_wait_event(thread->t_ctl_waitq,
945 !cfs_list_empty(&scrub->os_inconsistent_items) ||
946 !thread_is_running(thread),
950 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_CRASH)) {
951 spin_lock(&scrub->os_lock);
952 thread_set_flags(thread, SVC_STOPPING);
953 spin_unlock(&scrub->os_lock);
954 return SCRUB_NEXT_CRASH;
957 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_FATAL))
958 return SCRUB_NEXT_FATAL;
960 if (unlikely(!thread_is_running(thread)))
961 return SCRUB_NEXT_EXIT;
963 if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
964 struct osd_inconsistent_item *oii;
966 oii = cfs_list_entry(scrub->os_inconsistent_items.next,
967 struct osd_inconsistent_item, oii_list);
968 *oic = &oii->oii_cache;
969 scrub->os_in_prior = 1;
974 return SCRUB_NEXT_WAIT;
976 rc = osd_iit_next(param, &scrub->os_pos_current);
980 *oic = &scrub->os_oic;
981 fid = &(*oic)->oic_fid;
982 lid = &(*oic)->oic_lid;
983 rc = osd_iit_iget(info, dev, fid, lid,
984 scrub->os_pos_current, param->sb, true);
988 static int osd_preload_next(struct osd_thread_info *info,
989 struct osd_device *dev, struct osd_iit_param *param,
990 struct osd_idmap_cache **oic, int noslot)
992 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
993 struct osd_scrub *scrub;
994 struct ptlrpc_thread *thread;
997 rc = osd_iit_next(param, &ooc->ooc_pos_preload);
1001 scrub = &dev->od_scrub;
1002 thread = &scrub->os_thread;
1003 if (thread_is_running(thread) &&
1004 ooc->ooc_pos_preload >= scrub->os_pos_current)
1005 return SCRUB_NEXT_EXIT;
1007 rc = osd_iit_iget(info, dev,
1008 &ooc->ooc_cache[ooc->ooc_producer_idx].oic_fid,
1009 &ooc->ooc_cache[ooc->ooc_producer_idx].oic_lid,
1010 ooc->ooc_pos_preload, param->sb, false);
1011 /* If succeed, it needs to move forward; otherwise up layer LFSCK may
1012 * ignore the failure, so it still need to skip the inode next time. */
1013 ooc->ooc_pos_preload = param->gbase + ++(param->offset);
1018 osd_scrub_wakeup(struct osd_scrub *scrub, struct osd_otable_it *it)
1020 spin_lock(&scrub->os_lock);
1021 if (osd_scrub_has_window(scrub, &it->ooi_cache) ||
1022 !cfs_list_empty(&scrub->os_inconsistent_items) ||
1023 it->ooi_waiting || !thread_is_running(&scrub->os_thread))
1024 scrub->os_waiting = 0;
1026 scrub->os_waiting = 1;
1027 spin_unlock(&scrub->os_lock);
1029 return !scrub->os_waiting;
1032 static int osd_scrub_exec(struct osd_thread_info *info, struct osd_device *dev,
1033 struct osd_iit_param *param,
1034 struct osd_idmap_cache *oic, int *noslot, int rc)
1036 struct l_wait_info lwi = { 0 };
1037 struct osd_scrub *scrub = &dev->od_scrub;
1038 struct scrub_file *sf = &scrub->os_file;
1039 struct ptlrpc_thread *thread = &scrub->os_thread;
1040 struct osd_otable_it *it = dev->od_otable_it;
1041 struct osd_otable_cache *ooc = it ? &it->ooi_cache : NULL;
1044 case SCRUB_NEXT_CONTINUE:
1046 case SCRUB_NEXT_WAIT:
1048 case SCRUB_NEXT_NOSCRUB:
1049 down_write(&scrub->os_rwsem);
1050 scrub->os_new_checked++;
1051 sf->sf_items_noscrub++;
1052 up_write(&scrub->os_rwsem);
1056 rc = osd_scrub_check_update(info, dev, oic, rc);
1060 rc = osd_scrub_checkpoint(scrub);
1062 CERROR("%.16s: fail to checkpoint, pos = %u, rc = %d\n",
1063 LDISKFS_SB(param->sb)->s_es->s_volume_name,
1064 scrub->os_pos_current, rc);
1065 /* Continue, as long as the scrub itself can go ahead. */
1068 if (scrub->os_in_prior) {
1069 scrub->os_in_prior = 0;
1074 scrub->os_pos_current = param->gbase + ++(param->offset);
1077 if (it != NULL && it->ooi_waiting && ooc != NULL &&
1078 ooc->ooc_pos_preload < scrub->os_pos_current) {
1079 spin_lock(&scrub->os_lock);
1080 it->ooi_waiting = 0;
1081 wake_up_all(&thread->t_ctl_waitq);
1082 spin_unlock(&scrub->os_lock);
1085 if (scrub->os_full_speed || rc == SCRUB_NEXT_CONTINUE)
1088 if (osd_scrub_has_window(scrub, ooc)) {
1093 l_wait_event(thread->t_ctl_waitq,
1094 osd_scrub_wakeup(scrub, it),
1097 if (osd_scrub_has_window(scrub, ooc))
1104 static int osd_preload_exec(struct osd_thread_info *info,
1105 struct osd_device *dev, struct osd_iit_param *param,
1106 struct osd_idmap_cache *oic, int *noslot, int rc)
1108 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
1111 ooc->ooc_cached_items++;
1112 ooc->ooc_producer_idx = (ooc->ooc_producer_idx + 1) &
1113 ~OSD_OTABLE_IT_CACHE_MASK;
1115 return rc > 0 ? 0 : rc;
1118 #define SCRUB_IT_ALL 1
1119 #define SCRUB_IT_CRASH 2
1121 static int osd_inode_iteration(struct osd_thread_info *info,
1122 struct osd_device *dev, __u32 max, bool preload)
1124 osd_iit_next_policy next;
1125 osd_iit_exec_policy exec;
1128 struct osd_iit_param param;
1135 struct osd_scrub *scrub = &dev->od_scrub;
1137 next = osd_scrub_next;
1138 exec = osd_scrub_exec;
1139 pos = &scrub->os_pos_current;
1140 count = &scrub->os_new_checked;
1142 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
1144 next = osd_preload_next;
1145 exec = osd_preload_exec;
1146 pos = &ooc->ooc_pos_preload;
1147 count = &ooc->ooc_cached_items;
1149 param.sb = osd_sb(dev);
1150 limit = le32_to_cpu(LDISKFS_SB(param.sb)->s_es->s_inodes_count);
1152 while (*pos <= limit && *count < max) {
1153 struct osd_idmap_cache *oic = NULL;
1155 param.bg = (*pos - 1) / LDISKFS_INODES_PER_GROUP(param.sb);
1156 param.offset = (*pos - 1) % LDISKFS_INODES_PER_GROUP(param.sb);
1157 param.gbase = 1 + param.bg * LDISKFS_INODES_PER_GROUP(param.sb);
1158 param.bitmap = ldiskfs_read_inode_bitmap(param.sb, param.bg);
1159 if (param.bitmap == NULL) {
1160 CERROR("%.16s: fail to read bitmap for %u, "
1161 "scrub will stop, urgent mode\n",
1162 LDISKFS_SB(param.sb)->s_es->s_volume_name,
1167 while (param.offset < LDISKFS_INODES_PER_GROUP(param.sb) &&
1169 rc = next(info, dev, ¶m, &oic, noslot);
1171 case SCRUB_NEXT_BREAK:
1173 case SCRUB_NEXT_EXIT:
1174 brelse(param.bitmap);
1176 case SCRUB_NEXT_CRASH:
1177 brelse(param.bitmap);
1178 RETURN(SCRUB_IT_CRASH);
1179 case SCRUB_NEXT_FATAL:
1180 brelse(param.bitmap);
1184 rc = exec(info, dev, ¶m, oic, &noslot, rc);
1186 brelse(param.bitmap);
1192 brelse(param.bitmap);
1196 RETURN(SCRUB_IT_ALL);
1200 static int osd_otable_it_preload(const struct lu_env *env,
1201 struct osd_otable_it *it)
1203 struct osd_device *dev = it->ooi_dev;
1204 struct osd_scrub *scrub = &dev->od_scrub;
1205 struct osd_otable_cache *ooc = &it->ooi_cache;
1209 rc = osd_inode_iteration(osd_oti_get(env), dev,
1210 OSD_OTABLE_IT_CACHE_SIZE, true);
1211 if (rc == SCRUB_IT_ALL)
1212 it->ooi_all_cached = 1;
1214 CDEBUG(D_LFSCK, "OSD pre-loaded: max = %u, preload = %u, rc = %d\n",
1215 le32_to_cpu(LDISKFS_SB(osd_sb(dev))->s_es->s_inodes_count),
1216 ooc->ooc_pos_preload, rc);
1218 if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
1219 spin_lock(&scrub->os_lock);
1220 scrub->os_waiting = 0;
1221 wake_up_all(&scrub->os_thread.t_ctl_waitq);
1222 spin_unlock(&scrub->os_lock);
1225 RETURN(rc < 0 ? rc : ooc->ooc_cached_items);
1228 static int osd_scrub_main(void *args)
1231 struct osd_device *dev = (struct osd_device *)args;
1232 struct osd_scrub *scrub = &dev->od_scrub;
1233 struct ptlrpc_thread *thread = &scrub->os_thread;
1234 struct super_block *sb = osd_sb(dev);
1238 rc = lu_env_init(&env, LCT_LOCAL);
1240 CERROR("%.16s: OI scrub, fail to init env, rc = %d\n",
1241 LDISKFS_SB(sb)->s_es->s_volume_name, rc);
1245 rc = osd_scrub_prep(dev);
1247 CERROR("%.16s: OI scrub, fail to scrub prep, rc = %d\n",
1248 LDISKFS_SB(sb)->s_es->s_volume_name, rc);
1252 if (!scrub->os_full_speed) {
1253 struct l_wait_info lwi = { 0 };
1254 struct osd_otable_it *it = dev->od_otable_it;
1255 struct osd_otable_cache *ooc = &it->ooi_cache;
1257 l_wait_event(thread->t_ctl_waitq,
1258 it->ooi_user_ready || !thread_is_running(thread),
1260 if (unlikely(!thread_is_running(thread)))
1263 scrub->os_pos_current = ooc->ooc_pos_preload;
1266 CDEBUG(D_LFSCK, "OI scrub: flags = 0x%x, pos = %u\n",
1267 scrub->os_start_flags, scrub->os_pos_current);
1269 rc = osd_inode_iteration(osd_oti_get(&env), dev, ~0U, false);
1270 if (unlikely(rc == SCRUB_IT_CRASH))
1271 GOTO(out, rc = -EINVAL);
1275 osd_scrub_post(scrub, rc);
1276 CDEBUG(D_LFSCK, "OI scrub: stop, rc = %d, pos = %u\n",
1277 rc, scrub->os_pos_current);
1280 while (!cfs_list_empty(&scrub->os_inconsistent_items)) {
1281 struct osd_inconsistent_item *oii;
1283 oii = cfs_list_entry(scrub->os_inconsistent_items.next,
1284 struct osd_inconsistent_item, oii_list);
1285 cfs_list_del_init(&oii->oii_list);
1291 spin_lock(&scrub->os_lock);
1292 thread_set_flags(thread, SVC_STOPPED);
1293 wake_up_all(&thread->t_ctl_waitq);
1294 spin_unlock(&scrub->os_lock);
1298 /* initial OI scrub */
1300 typedef int (*scandir_t)(struct osd_thread_info *, struct osd_device *,
1301 struct dentry *, filldir_t filldir);
1303 static int osd_ios_varfid_fill(void *buf, const char *name, int namelen,
1304 loff_t offset, __u64 ino, unsigned d_type);
1305 static int osd_ios_lf_fill(void *buf, const char *name, int namelen,
1306 loff_t offset, __u64 ino, unsigned d_type);
1309 osd_ios_general_scan(struct osd_thread_info *info, struct osd_device *dev,
1310 struct dentry *dentry, filldir_t filldir);
1312 osd_ios_ROOT_scan(struct osd_thread_info *info, struct osd_device *dev,
1313 struct dentry *dentry, filldir_t filldir);
1316 osd_ios_OBJECTS_scan(struct osd_thread_info *info, struct osd_device *dev,
1317 struct dentry *dentry, filldir_t filldir);
1320 OLF_SCAN_SUBITEMS = 0x0001,
1321 OLF_HIDE_FID = 0x0002,
1322 OLF_SHOW_NAME = 0x0004,
1328 struct lu_fid olm_fid;
1330 scandir_t olm_scandir;
1331 filldir_t olm_filldir;
1334 /* Add the new introduced local files in the list in the future. */
1335 static const struct osd_lf_map osd_lf_maps[] = {
1337 { CATLIST, { FID_SEQ_LOCAL_FILE, LLOG_CATALOGS_OID, 0 }, OLF_SHOW_NAME,
1341 { MOUNT_CONFIGS_DIR, { FID_SEQ_LOCAL_FILE, MGS_CONFIGS_OID, 0 },
1342 OLF_SCAN_SUBITEMS, osd_ios_general_scan,
1343 osd_ios_varfid_fill },
1345 /* NIDTBL_VERSIONS */
1346 { MGS_NIDTBL_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1347 osd_ios_general_scan, osd_ios_varfid_fill },
1350 { "PENDING", { 0, 0, 0 }, 0, NULL, NULL },
1353 { "ROOT", { FID_SEQ_ROOT, 1, 0 },
1354 OLF_SCAN_SUBITEMS | OLF_HIDE_FID, osd_ios_ROOT_scan, NULL },
1356 /* changelog_catalog */
1357 { CHANGELOG_CATALOG, { 0, 0, 0 }, 0, NULL, NULL },
1359 /* changelog_users */
1360 { CHANGELOG_USERS, { 0, 0, 0 }, 0, NULL, NULL },
1363 { "fld", { FID_SEQ_LOCAL_FILE, FLD_INDEX_OID, 0 }, OLF_SHOW_NAME,
1367 { LAST_RCVD, { FID_SEQ_LOCAL_FILE, LAST_RECV_OID, 0 }, OLF_SHOW_NAME,
1370 /* lfsck_bookmark */
1371 { "lfsck_bookmark", { 0, 0, 0 }, 0, NULL, NULL },
1374 { LOV_OBJID, { FID_SEQ_LOCAL_FILE, MDD_LOV_OBJ_OID, 0 }, OLF_SHOW_NAME,
1378 { LOV_OBJSEQ, { FID_SEQ_LOCAL_FILE, MDD_LOV_OBJ_OSEQ, 0 },
1379 OLF_SHOW_NAME, NULL, NULL },
1382 { QMT_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1383 osd_ios_general_scan, osd_ios_varfid_fill },
1386 { QSD_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1387 osd_ios_general_scan, osd_ios_varfid_fill },
1390 { "seq_ctl", { FID_SEQ_LOCAL_FILE, FID_SEQ_CTL_OID, 0 },
1391 OLF_SHOW_NAME, NULL, NULL },
1394 { "seq_srv", { FID_SEQ_LOCAL_FILE, FID_SEQ_SRV_OID, 0 },
1395 OLF_SHOW_NAME, NULL, NULL },
1398 { HEALTH_CHECK, { FID_SEQ_LOCAL_FILE, OFD_HEALTH_CHECK_OID, 0 },
1399 OLF_SHOW_NAME, NULL, NULL },
1401 /* lfsck_namespace */
1402 { "lfsck_namespace", { 0, 0, 0 }, 0, NULL, NULL },
1404 /* OBJECTS, upgrade from old device */
1405 { OBJECTS, { 0, 0, 0 }, OLF_SCAN_SUBITEMS, osd_ios_OBJECTS_scan, NULL },
1407 /* lquota_v2.user, upgrade from old device */
1408 { "lquota_v2.user", { 0, 0, 0 }, 0, NULL, NULL },
1410 /* lquota_v2.group, upgrade from old device */
1411 { "lquota_v2.group", { 0, 0, 0 }, 0, NULL, NULL },
1413 /* LAST_GROUP, upgrade from old device */
1414 { "LAST_GROUP", { FID_SEQ_LOCAL_FILE, OFD_LAST_GROUP_OID, 0 },
1415 OLF_SHOW_NAME, NULL, NULL },
1418 { "lost+found", { 0, 0, 0 }, OLF_SCAN_SUBITEMS | OLF_NO_OI,
1419 osd_ios_general_scan, osd_ios_lf_fill },
1421 { NULL, { 0, 0, 0 }, 0, NULL, NULL }
1424 struct osd_ios_item {
1425 cfs_list_t oii_list;
1426 struct dentry *oii_dentry;
1427 scandir_t oii_scandir;
1428 filldir_t oii_filldir;
1431 struct osd_ios_filldir_buf {
1432 struct osd_thread_info *oifb_info;
1433 struct osd_device *oifb_dev;
1434 struct dentry *oifb_dentry;
1437 static inline struct dentry *
1438 osd_ios_lookup_one_len(const char *name, struct dentry *parent, int namelen)
1440 struct dentry *dentry;
1442 dentry = ll_lookup_one_len(name, parent, namelen);
1443 if (!IS_ERR(dentry) && dentry->d_inode == NULL) {
1445 return ERR_PTR(-ENOENT);
1452 osd_ios_new_item(struct osd_device *dev, struct dentry *dentry,
1453 scandir_t scandir, filldir_t filldir)
1455 struct osd_ios_item *item;
1457 OBD_ALLOC_PTR(item);
1461 CFS_INIT_LIST_HEAD(&item->oii_list);
1462 item->oii_dentry = dget(dentry);
1463 item->oii_scandir = scandir;
1464 item->oii_filldir = filldir;
1465 cfs_list_add_tail(&item->oii_list, &dev->od_ios_list);
1470 * osd_ios_scan_one() - check/fix LMA FID and OI entry for one inode
1472 * The passed \a inode's \a fid is verified against the LMA FID. If the \a fid
1473 * is NULL or is empty the IGIF FID is used. The FID is verified in the OI to
1474 * reference the inode, or fixed if it is missing or references another inode.
1477 osd_ios_scan_one(struct osd_thread_info *info, struct osd_device *dev,
1478 struct inode *inode, const struct lu_fid *fid, int flags)
1480 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
1481 struct osd_inode_id *id = &info->oti_id;
1482 struct osd_inode_id *id2 = &info->oti_id2;
1483 struct osd_scrub *scrub = &dev->od_scrub;
1484 struct scrub_file *sf = &scrub->os_file;
1489 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
1490 if (rc != 0 && rc != -ENODATA)
1493 osd_id_gen(id, inode->i_ino, inode->i_generation);
1494 if (rc == -ENODATA) {
1495 if (fid == NULL || fid_is_zero(fid) || flags & OLF_HIDE_FID)
1496 lu_igif_build(&tfid, inode->i_ino, inode->i_generation);
1499 rc = osd_ea_fid_set(info, inode, &tfid, 0, 0);
1503 if (lma->lma_compat & LMAC_NOT_IN_OI)
1506 tfid = lma->lma_self_fid;
1509 rc = osd_oi_lookup(info, dev, &tfid, id2, 0);
1514 rc = osd_scrub_refresh_mapping(info, dev, &tfid, id,
1515 DTO_INDEX_INSERT, 0);
1522 if (osd_id_eq_strict(id, id2))
1525 if (!(sf->sf_flags & SF_INCONSISTENT)) {
1526 osd_scrub_file_reset(scrub,
1527 LDISKFS_SB(osd_sb(dev))->s_es->s_uuid,
1529 rc = osd_scrub_file_store(scrub);
1534 rc = osd_scrub_refresh_mapping(info, dev, &tfid, id,
1535 DTO_INDEX_UPDATE, 0);
1543 * It scans the /lost+found, and for the OST-object (with filter_fid
1544 * or filter_fid_old), move them back to its proper /O/<seq>/d<x>.
1546 static int osd_ios_lf_fill(void *buf, const char *name, int namelen,
1547 loff_t offset, __u64 ino, unsigned d_type)
1549 struct osd_ios_filldir_buf *fill_buf = buf;
1550 struct osd_thread_info *info = fill_buf->oifb_info;
1551 struct osd_device *dev = fill_buf->oifb_dev;
1552 struct lu_fid *fid = &info->oti_fid;
1553 struct osd_scrub *scrub = &dev->od_scrub;
1554 struct dentry *parent = fill_buf->oifb_dentry;
1555 struct dentry *child;
1556 struct inode *dir = parent->d_inode;
1557 struct inode *inode;
1561 /* skip any '.' started names */
1565 scrub->os_lf_scanned++;
1566 child = osd_ios_lookup_one_len(name, parent, namelen);
1567 if (IS_ERR(child)) {
1568 CWARN("%s: cannot lookup child '%.*s': rc = %d\n",
1569 osd_name(dev), namelen, name, (int)PTR_ERR(child));
1573 inode = child->d_inode;
1574 if (S_ISDIR(inode->i_mode)) {
1575 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
1578 CWARN("%s: cannot add child '%.*s': rc = %d\n",
1579 osd_name(dev), namelen, name, rc);
1583 if (!S_ISREG(inode->i_mode))
1586 rc = osd_scrub_get_fid(info, dev, inode, fid, true);
1587 if (rc == SCRUB_NEXT_OSTOBJ || rc == SCRUB_NEXT_OSTOBJ_OLD) {
1588 rc = osd_obj_map_recover(info, dev, dir, child, fid);
1590 CDEBUG(D_LFSCK, "recovered '%.*s' ["DFID"] from "
1591 "/lost+found.\n", namelen, name, PFID(fid));
1592 scrub->os_lf_repaired++;
1594 CWARN("%s: cannot rename for '%.*s' "DFID": rc = %d\n",
1595 osd_name(dev), namelen, name, PFID(fid), rc);
1599 /* XXX: For MDT-objects, we can move them from /lost+found to namespace
1600 * visible place, such as the /ROOT/.lustre/lost+found, then LFSCK
1601 * can process them in furtuer. */
1607 scrub->os_lf_failed++;
1609 /* skip the failure to make the scanning to continue. */
1613 static int osd_ios_varfid_fill(void *buf, const char *name, int namelen,
1614 loff_t offset, __u64 ino, unsigned d_type)
1616 struct osd_ios_filldir_buf *fill_buf = buf;
1617 struct osd_device *dev = fill_buf->oifb_dev;
1618 struct dentry *child;
1622 /* skip any '.' started names */
1626 child = osd_ios_lookup_one_len(name, fill_buf->oifb_dentry, namelen);
1628 RETURN(PTR_ERR(child));
1630 rc = osd_ios_scan_one(fill_buf->oifb_info, dev, child->d_inode,
1632 if (rc == 0 && S_ISDIR(child->d_inode->i_mode))
1633 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
1634 osd_ios_varfid_fill);
1640 static int osd_ios_root_fill(void *buf, const char *name, int namelen,
1641 loff_t offset, __u64 ino, unsigned d_type)
1643 struct osd_ios_filldir_buf *fill_buf = buf;
1644 struct osd_device *dev = fill_buf->oifb_dev;
1645 const struct osd_lf_map *map;
1646 struct dentry *child;
1650 /* skip any '.' started names */
1654 for (map = osd_lf_maps; map->olm_name != NULL; map++) {
1655 if (strlen(map->olm_name) != namelen)
1658 if (strncmp(map->olm_name, name, namelen) == 0)
1662 if (map->olm_name == NULL)
1665 child = osd_ios_lookup_one_len(name, fill_buf->oifb_dentry, namelen);
1667 RETURN(PTR_ERR(child));
1669 if (!(map->olm_flags & OLF_NO_OI))
1670 rc = osd_ios_scan_one(fill_buf->oifb_info, dev, child->d_inode,
1671 &map->olm_fid, map->olm_flags);
1672 if (rc == 0 && map->olm_flags & OLF_SCAN_SUBITEMS)
1673 rc = osd_ios_new_item(dev, child, map->olm_scandir,
1681 osd_ios_general_scan(struct osd_thread_info *info, struct osd_device *dev,
1682 struct dentry *dentry, filldir_t filldir)
1684 struct osd_ios_filldir_buf buf = { info, dev, dentry };
1685 struct file *filp = &info->oti_it_ea.oie_file;
1686 struct inode *inode = dentry->d_inode;
1687 const struct file_operations *fops = inode->i_fop;
1691 LASSERT(filldir != NULL);
1694 filp->f_dentry = dentry;
1695 filp->f_mode = FMODE_64BITHASH;
1696 filp->f_mapping = inode->i_mapping;
1698 filp->private_data = NULL;
1700 rc = fops->readdir(filp, &buf, filldir);
1701 fops->release(inode, filp);
1707 osd_ios_ROOT_scan(struct osd_thread_info *info, struct osd_device *dev,
1708 struct dentry *dentry, filldir_t filldir)
1710 struct osd_scrub *scrub = &dev->od_scrub;
1711 struct scrub_file *sf = &scrub->os_file;
1712 struct dentry *child;
1716 /* It is existing MDT0 device. We only allow the case of object without
1717 * LMA to happen on the MDT0, which is usually for old 1.8 MDT. Then we
1718 * can generate IGIF mode FID for the object and related OI mapping. If
1719 * it is on other MDTs, then becuase file-level backup/restore, related
1720 * OI mapping may be invalid already, we do not know which is the right
1721 * FID for the object. We only allow IGIF objects to reside on the MDT0.
1723 * XXX: For the case of object on non-MDT0 device with neither LMA nor
1724 * "fid" xattr, then something crashed. We cannot re-generate the
1725 * FID directly, instead, the OI scrub will scan the OI structure
1726 * and try to re-generate the LMA from the OI mapping. But if the
1727 * OI mapping crashed or lost also, then we have to give up under
1728 * double failure cases. */
1729 scrub->os_convert_igif = 1;
1730 child = osd_ios_lookup_one_len(dot_lustre_name, dentry,
1731 strlen(dot_lustre_name));
1732 if (IS_ERR(child)) {
1733 rc = PTR_ERR(child);
1734 if (rc == -ENOENT) {
1735 /* It is 1.8 MDT device. */
1736 if (!(sf->sf_flags & SF_UPGRADE)) {
1737 osd_scrub_file_reset(scrub,
1738 LDISKFS_SB(osd_sb(dev))->s_es->s_uuid,
1740 sf->sf_internal_flags &= ~SIF_NO_HANDLE_OLD_FID;
1741 rc = osd_scrub_file_store(scrub);
1747 /* For lustre-2.x (x <= 3), the ".lustre" has NO FID-in-LMA,
1748 * so the client will get IGIF for the ".lustre" object when
1751 * From the OI scrub view, when the MDT upgrade to Lustre-2.4,
1752 * it does not know whether there are some old clients cached
1753 * the ".lustre" IGIF during the upgrading. Two choices:
1755 * 1) Generate IGIF-in-LMA and IGIF-in-OI for the ".lustre".
1756 * It will allow the old connected clients to access the
1757 * ".lustre" with cached IGIF. But it will cause others
1758 * on the MDT failed to check "fid_is_dot_lustre()".
1760 * 2) Use fixed FID {FID_SEQ_DOT_LUSTRE, FID_OID_DOT_LUSTRE, 0}
1761 * for ".lustre" in spite of whether there are some clients
1762 * cached the ".lustre" IGIF or not. It enables the check
1763 * "fid_is_dot_lustre()" on the MDT, although it will cause
1764 * that the old connected clients cannot access the ".lustre"
1765 * with the cached IGIF.
1767 * Usually, it is rare case for the old connected clients
1768 * to access the ".lustre" with cached IGIF. So we prefer
1769 * to the solution 2). */
1770 rc = osd_ios_scan_one(info, dev, child->d_inode,
1771 &LU_DOT_LUSTRE_FID, 0);
1779 osd_ios_OBJECTS_scan(struct osd_thread_info *info, struct osd_device *dev,
1780 struct dentry *dentry, filldir_t filldir)
1782 struct osd_scrub *scrub = &dev->od_scrub;
1783 struct scrub_file *sf = &scrub->os_file;
1784 struct dentry *child;
1788 if (unlikely(sf->sf_internal_flags & SIF_NO_HANDLE_OLD_FID)) {
1789 sf->sf_internal_flags &= ~SIF_NO_HANDLE_OLD_FID;
1790 rc = osd_scrub_file_store(scrub);
1795 child = osd_ios_lookup_one_len(ADMIN_USR, dentry, strlen(ADMIN_USR));
1796 if (!IS_ERR(child)) {
1797 rc = osd_ios_scan_one(info, dev, child->d_inode, NULL, 0);
1800 rc = PTR_ERR(child);
1803 if (rc != 0 && rc != -ENOENT)
1806 child = osd_ios_lookup_one_len(ADMIN_GRP, dentry, strlen(ADMIN_GRP));
1807 if (!IS_ERR(child)) {
1808 rc = osd_ios_scan_one(info, dev, child->d_inode, NULL, 0);
1811 rc = PTR_ERR(child);
1820 static int osd_initial_OI_scrub(struct osd_thread_info *info,
1821 struct osd_device *dev)
1823 struct osd_ios_item *item = NULL;
1824 scandir_t scandir = osd_ios_general_scan;
1825 filldir_t filldir = osd_ios_root_fill;
1826 struct dentry *dentry = osd_sb(dev)->s_root;
1827 const struct osd_lf_map *map = osd_lf_maps;
1832 rc = scandir(info, dev, dentry, filldir);
1834 dput(item->oii_dentry);
1841 if (cfs_list_empty(&dev->od_ios_list))
1844 item = cfs_list_entry(dev->od_ios_list.next,
1845 struct osd_ios_item, oii_list);
1846 cfs_list_del_init(&item->oii_list);
1848 LASSERT(item->oii_scandir != NULL);
1849 scandir = item->oii_scandir;
1850 filldir = item->oii_filldir;
1851 dentry = item->oii_dentry;
1854 while (!cfs_list_empty(&dev->od_ios_list)) {
1855 item = cfs_list_entry(dev->od_ios_list.next,
1856 struct osd_ios_item, oii_list);
1857 cfs_list_del_init(&item->oii_list);
1858 dput(item->oii_dentry);
1865 /* There maybe the case that the object has been removed, but its OI
1866 * mapping is still in the OI file, such as the "CATALOGS" after MDT
1867 * file-level backup/restore. So here cleanup the stale OI mappings. */
1868 while (map->olm_name != NULL) {
1869 struct dentry *child;
1871 if (fid_is_zero(&map->olm_fid)) {
1876 child = osd_ios_lookup_one_len(map->olm_name,
1877 osd_sb(dev)->s_root,
1878 strlen(map->olm_name));
1881 else if (PTR_ERR(child) == -ENOENT)
1882 osd_scrub_refresh_mapping(info, dev, &map->olm_fid,
1883 NULL, DTO_INDEX_DELETE, 0);
1890 char *osd_lf_fid2name(const struct lu_fid *fid)
1892 const struct osd_lf_map *map = osd_lf_maps;
1894 while (map->olm_name != NULL) {
1895 if (!lu_fid_eq(fid, &map->olm_fid)) {
1900 if (map->olm_flags & OLF_SHOW_NAME)
1901 return map->olm_name;
1909 /* OI scrub start/stop */
1911 static int do_osd_scrub_start(struct osd_device *dev, __u32 flags)
1913 struct osd_scrub *scrub = &dev->od_scrub;
1914 struct ptlrpc_thread *thread = &scrub->os_thread;
1915 struct l_wait_info lwi = { 0 };
1920 /* os_lock: sync status between stop and scrub thread */
1921 spin_lock(&scrub->os_lock);
1922 if (thread_is_running(thread)) {
1923 spin_unlock(&scrub->os_lock);
1925 } else if (unlikely(thread_is_stopping(thread))) {
1926 spin_unlock(&scrub->os_lock);
1927 l_wait_event(thread->t_ctl_waitq,
1928 thread_is_stopped(thread),
1932 spin_unlock(&scrub->os_lock);
1934 if (scrub->os_file.sf_status == SS_COMPLETED)
1937 scrub->os_start_flags = flags;
1938 thread_set_flags(thread, 0);
1939 rc = PTR_ERR(kthread_run(osd_scrub_main, dev, "OI_scrub"));
1940 if (IS_ERR_VALUE(rc)) {
1941 CERROR("%.16s: cannot start iteration thread, rc = %d\n",
1942 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, rc);
1946 l_wait_event(thread->t_ctl_waitq,
1947 thread_is_running(thread) || thread_is_stopped(thread),
1953 int osd_scrub_start(struct osd_device *dev)
1958 /* od_otable_mutex: prevent curcurrent start/stop */
1959 mutex_lock(&dev->od_otable_mutex);
1960 rc = do_osd_scrub_start(dev, SS_AUTO);
1961 mutex_unlock(&dev->od_otable_mutex);
1963 RETURN(rc == -EALREADY ? 0 : rc);
1966 static void do_osd_scrub_stop(struct osd_scrub *scrub)
1968 struct ptlrpc_thread *thread = &scrub->os_thread;
1969 struct l_wait_info lwi = { 0 };
1971 /* os_lock: sync status between stop and scrub thread */
1972 spin_lock(&scrub->os_lock);
1973 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
1974 thread_set_flags(thread, SVC_STOPPING);
1975 spin_unlock(&scrub->os_lock);
1976 wake_up_all(&thread->t_ctl_waitq);
1977 l_wait_event(thread->t_ctl_waitq,
1978 thread_is_stopped(thread),
1980 /* Do not skip the last lock/unlock, which can guarantee that
1981 * the caller cannot return until the OI scrub thread exit. */
1982 spin_lock(&scrub->os_lock);
1984 spin_unlock(&scrub->os_lock);
1987 static void osd_scrub_stop(struct osd_device *dev)
1989 /* od_otable_mutex: prevent curcurrent start/stop */
1990 mutex_lock(&dev->od_otable_mutex);
1991 dev->od_scrub.os_paused = 1;
1992 do_osd_scrub_stop(&dev->od_scrub);
1993 mutex_unlock(&dev->od_otable_mutex);
1996 /* OI scrub setup/cleanup */
1998 static const char osd_scrub_name[] = "OI_scrub";
2000 int osd_scrub_setup(const struct lu_env *env, struct osd_device *dev)
2002 struct osd_thread_info *info = osd_oti_get(env);
2003 struct osd_scrub *scrub = &dev->od_scrub;
2004 struct lvfs_run_ctxt *ctxt = &scrub->os_ctxt;
2005 struct scrub_file *sf = &scrub->os_file;
2006 struct super_block *sb = osd_sb(dev);
2007 struct ldiskfs_super_block *es = LDISKFS_SB(sb)->s_es;
2008 struct lvfs_run_ctxt saved;
2010 struct inode *inode;
2011 struct lu_fid *fid = &info->oti_fid;
2016 memset(scrub, 0, sizeof(*scrub));
2017 OBD_SET_CTXT_MAGIC(ctxt);
2018 ctxt->pwdmnt = dev->od_mnt;
2019 ctxt->pwd = dev->od_mnt->mnt_root;
2020 ctxt->fs = get_ds();
2022 init_waitqueue_head(&scrub->os_thread.t_ctl_waitq);
2023 init_rwsem(&scrub->os_rwsem);
2024 spin_lock_init(&scrub->os_lock);
2025 CFS_INIT_LIST_HEAD(&scrub->os_inconsistent_items);
2027 push_ctxt(&saved, ctxt, NULL);
2028 filp = filp_open(osd_scrub_name, O_RDWR | O_CREAT, 0644);
2030 pop_ctxt(&saved, ctxt, NULL);
2031 RETURN(PTR_ERR(filp));
2034 inode = filp->f_dentry->d_inode;
2035 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
2036 /* 'What the @fid is' is not imporatant, because the object
2037 * has no OI mapping, and only is visible inside the OSD.*/
2038 lu_igif_build(fid, inode->i_ino, inode->i_generation);
2039 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
2041 filp_close(filp, 0);
2042 pop_ctxt(&saved, ctxt, NULL);
2046 scrub->os_inode = igrab(inode);
2047 filp_close(filp, 0);
2048 pop_ctxt(&saved, ctxt, NULL);
2050 rc = osd_scrub_file_load(scrub);
2051 if (rc == -ENOENT) {
2052 osd_scrub_file_init(scrub, es->s_uuid);
2053 /* If the "/O" dir does not exist when mount (indicated by
2054 * osd_device::od_maybe_new), neither for the "/OI_scrub",
2055 * then it is quite probably that the device is a new one,
2056 * under such case, mark it as SIF_NO_HANDLE_OLD_FID.
2058 * For the rare case that "/O" and "OI_scrub" both lost on
2059 * an old device, it can be found and cleared later.
2061 * For the system with "SIF_NO_HANDLE_OLD_FID", we do not
2062 * need to check "filter_fid_old" and to convert it to
2063 * "filter_fid" for each object, and all the IGIF should
2064 * have their FID mapping in OI files already. */
2065 if (dev->od_maybe_new)
2066 sf->sf_internal_flags = SIF_NO_HANDLE_OLD_FID;
2068 } else if (rc != 0) {
2071 if (memcmp(sf->sf_uuid, es->s_uuid, 16) != 0) {
2072 osd_scrub_file_reset(scrub, es->s_uuid,SF_INCONSISTENT);
2074 } else if (sf->sf_status == SS_SCANNING) {
2075 sf->sf_status = SS_CRASHED;
2080 if (sf->sf_pos_last_checkpoint != 0)
2081 scrub->os_pos_current = sf->sf_pos_last_checkpoint + 1;
2083 scrub->os_pos_current = LDISKFS_FIRST_INO(sb) + 1;
2086 rc = osd_scrub_file_store(scrub);
2091 /* Initialize OI files. */
2092 rc = osd_oi_init(info, dev);
2096 rc = osd_initial_OI_scrub(info, dev);
2098 if (sf->sf_flags & SF_UPGRADE ||
2099 !(sf->sf_internal_flags & SIF_NO_HANDLE_OLD_FID ||
2100 sf->sf_success_count > 0)) {
2101 dev->od_igif_inoi = 0;
2102 dev->od_check_ff = 1;
2104 dev->od_igif_inoi = 1;
2105 dev->od_check_ff = 0;
2108 if (sf->sf_flags & SF_INCONSISTENT)
2109 /* The 'od_igif_inoi' will be set under the
2111 * 1) new created system, or
2112 * 2) restored from file-level backup, or
2113 * 3) the upgrading completed.
2115 * The 'od_igif_inoi' may be cleared by OI scrub
2116 * later if found that the system is upgrading. */
2117 dev->od_igif_inoi = 1;
2119 if (!dev->od_noscrub &&
2120 ((sf->sf_status == SS_PAUSED) ||
2121 (sf->sf_status == SS_CRASHED &&
2122 sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
2123 SF_UPGRADE | SF_AUTO)) ||
2124 (sf->sf_status == SS_INIT &&
2125 sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
2127 rc = osd_scrub_start(dev);
2130 /* it is possible that dcache entries may keep objects after they are
2131 * deleted by OSD. While it looks safe this can cause object data to
2132 * stay until umount causing failures in tests calculating free space,
2133 * e.g. replay-ost-single. Since those dcache entries are not used
2134 * anymore let's just free them after use here */
2135 shrink_dcache_sb(sb);
2140 void osd_scrub_cleanup(const struct lu_env *env, struct osd_device *dev)
2142 struct osd_scrub *scrub = &dev->od_scrub;
2144 LASSERT(dev->od_otable_it == NULL);
2146 if (scrub->os_inode != NULL) {
2147 osd_scrub_stop(dev);
2148 iput(scrub->os_inode);
2149 scrub->os_inode = NULL;
2151 if (dev->od_oi_table != NULL)
2152 osd_oi_fini(osd_oti_get(env), dev);
2155 /* object table based iteration APIs */
2157 static struct dt_it *osd_otable_it_init(const struct lu_env *env,
2158 struct dt_object *dt, __u32 attr,
2159 struct lustre_capa *capa)
2161 enum dt_otable_it_flags flags = attr >> DT_OTABLE_IT_FLAGS_SHIFT;
2162 enum dt_otable_it_valid valid = attr & ~DT_OTABLE_IT_FLAGS_MASK;
2163 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
2164 struct osd_scrub *scrub = &dev->od_scrub;
2165 struct osd_otable_it *it;
2170 /* od_otable_mutex: prevent curcurrent init/fini */
2171 mutex_lock(&dev->od_otable_mutex);
2172 if (dev->od_otable_it != NULL)
2173 GOTO(out, it = ERR_PTR(-EALREADY));
2177 GOTO(out, it = ERR_PTR(-ENOMEM));
2179 dev->od_otable_it = it;
2181 it->ooi_cache.ooc_consumer_idx = -1;
2182 if (flags & DOIF_OUTUSED)
2183 it->ooi_used_outside = 1;
2185 if (flags & DOIF_RESET)
2188 if (valid & DOIV_ERROR_HANDLE) {
2189 if (flags & DOIF_FAILOUT)
2190 start |= SS_SET_FAILOUT;
2192 start |= SS_CLEAR_FAILOUT;
2195 rc = do_osd_scrub_start(dev, start);
2196 if (rc < 0 && rc != -EALREADY) {
2197 dev->od_otable_it = NULL;
2199 GOTO(out, it = ERR_PTR(rc));
2202 it->ooi_cache.ooc_pos_preload = scrub->os_pos_current;
2207 mutex_unlock(&dev->od_otable_mutex);
2208 return (struct dt_it *)it;
2211 static void osd_otable_it_fini(const struct lu_env *env, struct dt_it *di)
2213 struct osd_otable_it *it = (struct osd_otable_it *)di;
2214 struct osd_device *dev = it->ooi_dev;
2216 /* od_otable_mutex: prevent curcurrent init/fini */
2217 mutex_lock(&dev->od_otable_mutex);
2218 do_osd_scrub_stop(&dev->od_scrub);
2219 LASSERT(dev->od_otable_it == it);
2221 dev->od_otable_it = NULL;
2222 mutex_unlock(&dev->od_otable_mutex);
2226 static int osd_otable_it_get(const struct lu_env *env,
2227 struct dt_it *di, const struct dt_key *key)
2232 static void osd_otable_it_put(const struct lu_env *env, struct dt_it *di)
2237 osd_otable_it_wakeup(struct osd_scrub *scrub, struct osd_otable_it *it)
2239 spin_lock(&scrub->os_lock);
2240 if (it->ooi_cache.ooc_pos_preload < scrub->os_pos_current ||
2241 scrub->os_waiting ||
2242 !thread_is_running(&scrub->os_thread))
2243 it->ooi_waiting = 0;
2245 it->ooi_waiting = 1;
2246 spin_unlock(&scrub->os_lock);
2248 return !it->ooi_waiting;
2251 static int osd_otable_it_next(const struct lu_env *env, struct dt_it *di)
2253 struct osd_otable_it *it = (struct osd_otable_it *)di;
2254 struct osd_device *dev = it->ooi_dev;
2255 struct osd_scrub *scrub = &dev->od_scrub;
2256 struct osd_otable_cache *ooc = &it->ooi_cache;
2257 struct ptlrpc_thread *thread = &scrub->os_thread;
2258 struct l_wait_info lwi = { 0 };
2262 LASSERT(it->ooi_user_ready);
2265 if (!thread_is_running(thread) && !it->ooi_used_outside)
2268 if (ooc->ooc_cached_items > 0) {
2269 ooc->ooc_cached_items--;
2270 ooc->ooc_consumer_idx = (ooc->ooc_consumer_idx + 1) &
2271 ~OSD_OTABLE_IT_CACHE_MASK;
2275 if (it->ooi_all_cached) {
2276 l_wait_event(thread->t_ctl_waitq,
2277 !thread_is_running(thread),
2282 if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
2283 spin_lock(&scrub->os_lock);
2284 scrub->os_waiting = 0;
2285 wake_up_all(&scrub->os_thread.t_ctl_waitq);
2286 spin_unlock(&scrub->os_lock);
2289 if (it->ooi_cache.ooc_pos_preload >= scrub->os_pos_current)
2290 l_wait_event(thread->t_ctl_waitq,
2291 osd_otable_it_wakeup(scrub, it),
2294 if (!thread_is_running(thread) && !it->ooi_used_outside)
2297 rc = osd_otable_it_preload(env, it);
2304 static struct dt_key *osd_otable_it_key(const struct lu_env *env,
2305 const struct dt_it *di)
2310 static int osd_otable_it_key_size(const struct lu_env *env,
2311 const struct dt_it *di)
2313 return sizeof(__u64);
2316 static int osd_otable_it_rec(const struct lu_env *env, const struct dt_it *di,
2317 struct dt_rec *rec, __u32 attr)
2319 struct osd_otable_it *it = (struct osd_otable_it *)di;
2320 struct osd_otable_cache *ooc = &it->ooi_cache;
2322 *(struct lu_fid *)rec = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_fid;
2324 /* Filter out Invald FID already. */
2325 LASSERTF(fid_is_sane((struct lu_fid *)rec),
2326 "Invalid FID "DFID", p_idx = %d, c_idx = %d\n",
2327 PFID((struct lu_fid *)rec),
2328 ooc->ooc_producer_idx, ooc->ooc_consumer_idx);
2333 static __u64 osd_otable_it_store(const struct lu_env *env,
2334 const struct dt_it *di)
2336 struct osd_otable_it *it = (struct osd_otable_it *)di;
2337 struct osd_otable_cache *ooc = &it->ooi_cache;
2340 if (it->ooi_user_ready && ooc->ooc_consumer_idx != -1)
2341 hash = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_lid.oii_ino;
2343 hash = ooc->ooc_pos_preload;
2348 * Set the OSD layer iteration start position as the specified hash.
2350 static int osd_otable_it_load(const struct lu_env *env,
2351 const struct dt_it *di, __u64 hash)
2353 struct osd_otable_it *it = (struct osd_otable_it *)di;
2354 struct osd_device *dev = it->ooi_dev;
2355 struct osd_otable_cache *ooc = &it->ooi_cache;
2356 struct osd_scrub *scrub = &dev->od_scrub;
2360 /* Forbid to set iteration position after iteration started. */
2361 if (it->ooi_user_ready)
2364 if (hash > OSD_OTABLE_MAX_HASH)
2365 hash = OSD_OTABLE_MAX_HASH;
2367 ooc->ooc_pos_preload = hash;
2368 if (ooc->ooc_pos_preload <= LDISKFS_FIRST_INO(osd_sb(dev)))
2369 ooc->ooc_pos_preload = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
2371 it->ooi_user_ready = 1;
2372 if (!scrub->os_full_speed)
2373 wake_up_all(&scrub->os_thread.t_ctl_waitq);
2375 /* Unplug OSD layer iteration by the first next() call. */
2376 rc = osd_otable_it_next(env, (struct dt_it *)it);
2381 static int osd_otable_it_key_rec(const struct lu_env *env,
2382 const struct dt_it *di, void *key_rec)
2387 const struct dt_index_operations osd_otable_ops = {
2389 .init = osd_otable_it_init,
2390 .fini = osd_otable_it_fini,
2391 .get = osd_otable_it_get,
2392 .put = osd_otable_it_put,
2393 .next = osd_otable_it_next,
2394 .key = osd_otable_it_key,
2395 .key_size = osd_otable_it_key_size,
2396 .rec = osd_otable_it_rec,
2397 .store = osd_otable_it_store,
2398 .load = osd_otable_it_load,
2399 .key_rec = osd_otable_it_key_rec,
2403 /* high priority inconsistent items list APIs */
2405 int osd_oii_insert(struct osd_device *dev, struct osd_idmap_cache *oic,
2408 struct osd_inconsistent_item *oii;
2409 struct osd_scrub *scrub = &dev->od_scrub;
2410 struct ptlrpc_thread *thread = &scrub->os_thread;
2415 if (unlikely(oii == NULL))
2418 CFS_INIT_LIST_HEAD(&oii->oii_list);
2419 oii->oii_cache = *oic;
2420 oii->oii_insert = insert;
2422 spin_lock(&scrub->os_lock);
2423 if (unlikely(!thread_is_running(thread))) {
2424 spin_unlock(&scrub->os_lock);
2429 if (cfs_list_empty(&scrub->os_inconsistent_items))
2431 cfs_list_add_tail(&oii->oii_list, &scrub->os_inconsistent_items);
2432 spin_unlock(&scrub->os_lock);
2435 wake_up_all(&thread->t_ctl_waitq);
2440 int osd_oii_lookup(struct osd_device *dev, const struct lu_fid *fid,
2441 struct osd_inode_id *id)
2443 struct osd_scrub *scrub = &dev->od_scrub;
2444 struct osd_inconsistent_item *oii;
2447 spin_lock(&scrub->os_lock);
2448 cfs_list_for_each_entry(oii, &scrub->os_inconsistent_items, oii_list) {
2449 if (lu_fid_eq(fid, &oii->oii_cache.oic_fid)) {
2450 *id = oii->oii_cache.oic_lid;
2451 spin_unlock(&scrub->os_lock);
2455 spin_unlock(&scrub->os_lock);
2462 static const char *scrub_status_names[] = {
2473 static const char *scrub_flags_names[] = {
2481 static const char *scrub_param_names[] = {
2486 static int scrub_bits_dump(char **buf, int *len, int bits, const char *names[],
2494 rc = snprintf(*buf, *len, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
2500 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
2503 rc = snprintf(*buf, *len, "%s%c", names[i],
2504 bits != 0 ? ',' : '\n');
2515 static int scrub_time_dump(char **buf, int *len, __u64 time, const char *prefix)
2520 rc = snprintf(*buf, *len, "%s: "LPU64" seconds\n", prefix,
2521 cfs_time_current_sec() - time);
2523 rc = snprintf(*buf, *len, "%s: N/A\n", prefix);
2532 static int scrub_pos_dump(char **buf, int *len, __u64 pos, const char *prefix)
2537 rc = snprintf(*buf, *len, "%s: "LPU64"\n", prefix, pos);
2539 rc = snprintf(*buf, *len, "%s: N/A\n", prefix);
2548 int osd_scrub_dump(struct osd_device *dev, char *buf, int len)
2550 struct osd_scrub *scrub = &dev->od_scrub;
2551 struct scrub_file *sf = &scrub->os_file;
2558 down_read(&scrub->os_rwsem);
2559 rc = snprintf(buf, len,
2564 sf->sf_magic, (int)sf->sf_oi_count,
2565 scrub_status_names[sf->sf_status]);
2571 rc = scrub_bits_dump(&buf, &len, sf->sf_flags, scrub_flags_names,
2576 rc = scrub_bits_dump(&buf, &len, sf->sf_param, scrub_param_names,
2581 rc = scrub_time_dump(&buf, &len, sf->sf_time_last_complete,
2582 "time_since_last_completed");
2586 rc = scrub_time_dump(&buf, &len, sf->sf_time_latest_start,
2587 "time_since_latest_start");
2591 rc = scrub_time_dump(&buf, &len, sf->sf_time_last_checkpoint,
2592 "time_since_last_checkpoint");
2596 rc = scrub_pos_dump(&buf, &len, sf->sf_pos_latest_start,
2597 "latest_start_position");
2601 rc = scrub_pos_dump(&buf, &len, sf->sf_pos_last_checkpoint,
2602 "last_checkpoint_position");
2606 rc = scrub_pos_dump(&buf, &len, sf->sf_pos_first_inconsistent,
2607 "first_failure_position");
2611 checked = sf->sf_items_checked + scrub->os_new_checked;
2612 rc = snprintf(buf, len,
2613 "checked: "LPU64"\n"
2614 "updated: "LPU64"\n"
2616 "prior_updated: "LPU64"\n"
2617 "noscrub: "LPU64"\n"
2619 "success_count: %u\n",
2620 checked, sf->sf_items_updated, sf->sf_items_failed,
2621 sf->sf_items_updated_prior, sf->sf_items_noscrub,
2622 sf->sf_items_igif, sf->sf_success_count);
2629 if (thread_is_running(&scrub->os_thread)) {
2630 cfs_duration_t duration = cfs_time_current() -
2631 scrub->os_time_last_checkpoint;
2632 __u64 new_checked = scrub->os_new_checked * HZ;
2633 __u32 rtime = sf->sf_run_time +
2634 cfs_duration_sec(duration + HALF_SEC);
2637 do_div(new_checked, duration);
2639 do_div(speed, rtime);
2640 rc = snprintf(buf, len,
2641 "run_time: %u seconds\n"
2642 "average_speed: "LPU64" objects/sec\n"
2643 "real-time_speed: "LPU64" objects/sec\n"
2644 "current_position: %u\n"
2645 "lf_scanned: "LPU64"\n"
2646 "lf_reparied: "LPU64"\n"
2647 "lf_failed: "LPU64"\n",
2648 rtime, speed, new_checked, scrub->os_pos_current,
2649 scrub->os_lf_scanned, scrub->os_lf_repaired,
2650 scrub->os_lf_failed);
2652 if (sf->sf_run_time != 0)
2653 do_div(speed, sf->sf_run_time);
2654 rc = snprintf(buf, len,
2655 "run_time: %u seconds\n"
2656 "average_speed: "LPU64" objects/sec\n"
2657 "real-time_speed: N/A\n"
2658 "current_position: N/A\n"
2659 "lf_scanned: "LPU64"\n"
2660 "lf_reparied: "LPU64"\n"
2661 "lf_failed: "LPU64"\n",
2662 sf->sf_run_time, speed, scrub->os_lf_scanned,
2663 scrub->os_lf_repaired, scrub->os_lf_failed);
2673 up_read(&scrub->os_rwsem);