4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2012, 2013, Intel Corporation.
26 * lustre/osd-ldiskfs/osd_scrub.c
28 * Top-level entry points into osd module
30 * The OI scrub is used for rebuilding Object Index files when restores MDT from
33 * The otable based iterator scans ldiskfs inode table to feed up layer LFSCK.
35 * Author: Fan Yong <yong.fan@whamcloud.com>
38 #define DEBUG_SUBSYSTEM S_MDS
40 #include <lustre/lustre_idl.h>
41 #include <lustre_disk.h>
42 #include <dt_object.h>
43 #include <linux/xattr.h>
45 #include "osd_internal.h"
47 #include "osd_scrub.h"
49 #define HALF_SEC (HZ >> 1)
51 #define OSD_OTABLE_MAX_HASH 0x00000000ffffffffULL
53 #define SCRUB_NEXT_BREAK 1 /* exit current loop and process next group */
54 #define SCRUB_NEXT_CONTINUE 2 /* skip current object and process next bit */
55 #define SCRUB_NEXT_EXIT 3 /* exit all the loops */
56 #define SCRUB_NEXT_WAIT 4 /* wait for free cache slot */
57 #define SCRUB_NEXT_CRASH 5 /* simulate system crash during OI scrub */
58 #define SCRUB_NEXT_FATAL 6 /* simulate failure during OI scrub */
59 #define SCRUB_NEXT_NOSCRUB 7 /* new created object, no scrub on it */
60 #define SCRUB_NEXT_NOLMA 8 /* the inode has no FID-in-LMA */
61 #define SCRUB_NEXT_OSTOBJ 9 /* for OST-object */
62 #define SCRUB_NEXT_OSTOBJ_OLD 10 /* old OST-object, no LMA or no FID-on-OST
67 static inline struct osd_device *osd_scrub2dev(struct osd_scrub *scrub)
69 return container_of0(scrub, struct osd_device, od_scrub);
72 static inline struct super_block *osd_scrub2sb(struct osd_scrub *scrub)
74 return osd_sb(osd_scrub2dev(scrub));
77 static inline int osd_scrub_has_window(struct osd_scrub *scrub,
78 struct osd_otable_cache *ooc)
80 return scrub->os_pos_current < ooc->ooc_pos_preload + SCRUB_WINDOW_SIZE;
84 * update/insert/delete the specified OI mapping (@fid @id) according to the ops
86 * \retval 1, changed nothing
87 * \retval 0, changed successfully
88 * \retval -ve, on error
90 static int osd_scrub_refresh_mapping(struct osd_thread_info *info,
91 struct osd_device *dev,
92 const struct lu_fid *fid,
93 const struct osd_inode_id *id,
95 enum oi_check_flags flags)
101 if (dev->od_scrub.os_file.sf_param & SP_DRYRUN && !force)
104 /* DTO_INDEX_INSERT is enough for other two ops:
105 * delete/update, but save stack. */
106 th = ldiskfs_journal_start_sb(osd_sb(dev),
107 osd_dto_credits_noquota[DTO_INDEX_INSERT]);
110 CERROR("%s: fail to start trans for scrub %d: rc = %d\n",
111 osd_name(dev), ops, rc);
116 case DTO_INDEX_UPDATE:
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 rc = osd_oi_insert(info, dev, fid, id, th, flags);
125 if (unlikely(rc == -EEXIST)) {
127 /* XXX: There are trouble things when adding OI
128 * mapping for IGIF object, which may cause
129 * multiple objects to be mapped to the same
130 * IGIF formatted FID. Consider the following
133 * 1) The MDT is upgrading from 1.8 device.
134 * The OI scrub generates IGIF FID1 for the
135 * OBJ1 and adds the OI mapping.
137 * 2) For some reason, the OI scrub does not
138 * process all the IGIF objects completely.
140 * 3) The MDT is backuped and restored against
143 * 4) When the MDT mounts up, the OI scrub will
144 * try to rebuild the OI files. For some IGIF
145 * object, OBJ2, which was not processed by the
146 * OI scrub before the backup/restore, and the
147 * new generated IGIF formatted FID may be just
148 * the FID1, the same as OBJ1.
150 * Under such case, the OI scrub cannot know how
151 * to generate new FID for the OBJ2.
153 * Currently, we do nothing for that. One possible
154 * solution is to generate new normal FID for the
157 * Anyway, it is rare, only exists in theory. */
160 case DTO_INDEX_DELETE:
161 rc = osd_oi_delete(info, dev, fid, th, flags);
163 /* It is normal that the unlink thread has removed the
164 * OI mapping already. */
169 LASSERTF(0, "Unexpected ops %d\n", ops);
173 ldiskfs_journal_stop(th);
177 /* OI_scrub file ops */
179 static void osd_scrub_file_to_cpu(struct scrub_file *des,
180 struct scrub_file *src)
182 memcpy(des->sf_uuid, src->sf_uuid, 16);
183 des->sf_flags = le64_to_cpu(src->sf_flags);
184 des->sf_magic = le32_to_cpu(src->sf_magic);
185 des->sf_status = le16_to_cpu(src->sf_status);
186 des->sf_param = le16_to_cpu(src->sf_param);
187 des->sf_time_last_complete =
188 le64_to_cpu(src->sf_time_last_complete);
189 des->sf_time_latest_start =
190 le64_to_cpu(src->sf_time_latest_start);
191 des->sf_time_last_checkpoint =
192 le64_to_cpu(src->sf_time_last_checkpoint);
193 des->sf_pos_latest_start =
194 le64_to_cpu(src->sf_pos_latest_start);
195 des->sf_pos_last_checkpoint =
196 le64_to_cpu(src->sf_pos_last_checkpoint);
197 des->sf_pos_first_inconsistent =
198 le64_to_cpu(src->sf_pos_first_inconsistent);
199 des->sf_items_checked =
200 le64_to_cpu(src->sf_items_checked);
201 des->sf_items_updated =
202 le64_to_cpu(src->sf_items_updated);
203 des->sf_items_failed =
204 le64_to_cpu(src->sf_items_failed);
205 des->sf_items_updated_prior =
206 le64_to_cpu(src->sf_items_updated_prior);
207 des->sf_run_time = le32_to_cpu(src->sf_run_time);
208 des->sf_success_count = le32_to_cpu(src->sf_success_count);
209 des->sf_oi_count = le16_to_cpu(src->sf_oi_count);
210 des->sf_internal_flags = le16_to_cpu(src->sf_internal_flags);
211 memcpy(des->sf_oi_bitmap, src->sf_oi_bitmap, SCRUB_OI_BITMAP_SIZE);
214 static void osd_scrub_file_to_le(struct scrub_file *des,
215 struct scrub_file *src)
217 memcpy(des->sf_uuid, src->sf_uuid, 16);
218 des->sf_flags = cpu_to_le64(src->sf_flags);
219 des->sf_magic = cpu_to_le32(src->sf_magic);
220 des->sf_status = cpu_to_le16(src->sf_status);
221 des->sf_param = cpu_to_le16(src->sf_param);
222 des->sf_time_last_complete =
223 cpu_to_le64(src->sf_time_last_complete);
224 des->sf_time_latest_start =
225 cpu_to_le64(src->sf_time_latest_start);
226 des->sf_time_last_checkpoint =
227 cpu_to_le64(src->sf_time_last_checkpoint);
228 des->sf_pos_latest_start =
229 cpu_to_le64(src->sf_pos_latest_start);
230 des->sf_pos_last_checkpoint =
231 cpu_to_le64(src->sf_pos_last_checkpoint);
232 des->sf_pos_first_inconsistent =
233 cpu_to_le64(src->sf_pos_first_inconsistent);
234 des->sf_items_checked =
235 cpu_to_le64(src->sf_items_checked);
236 des->sf_items_updated =
237 cpu_to_le64(src->sf_items_updated);
238 des->sf_items_failed =
239 cpu_to_le64(src->sf_items_failed);
240 des->sf_items_updated_prior =
241 cpu_to_le64(src->sf_items_updated_prior);
242 des->sf_run_time = cpu_to_le32(src->sf_run_time);
243 des->sf_success_count = cpu_to_le32(src->sf_success_count);
244 des->sf_oi_count = cpu_to_le16(src->sf_oi_count);
245 des->sf_internal_flags = cpu_to_le16(src->sf_internal_flags);
246 memcpy(des->sf_oi_bitmap, src->sf_oi_bitmap, SCRUB_OI_BITMAP_SIZE);
249 static void osd_scrub_file_init(struct osd_scrub *scrub, __u8 *uuid)
251 struct scrub_file *sf = &scrub->os_file;
253 memset(sf, 0, sizeof(*sf));
254 memcpy(sf->sf_uuid, uuid, 16);
255 sf->sf_magic = SCRUB_MAGIC_V1;
256 sf->sf_status = SS_INIT;
259 void osd_scrub_file_reset(struct osd_scrub *scrub, __u8 *uuid, __u64 flags)
261 struct scrub_file *sf = &scrub->os_file;
263 CDEBUG(D_LFSCK, "Reset OI scrub file, flags = "LPX64"\n", flags);
264 memcpy(sf->sf_uuid, uuid, 16);
265 sf->sf_status = SS_INIT;
266 sf->sf_flags |= flags;
268 sf->sf_time_latest_start = 0;
269 sf->sf_time_last_checkpoint = 0;
270 sf->sf_pos_latest_start = 0;
271 sf->sf_pos_last_checkpoint = 0;
272 sf->sf_pos_first_inconsistent = 0;
273 sf->sf_items_checked = 0;
274 sf->sf_items_updated = 0;
275 sf->sf_items_failed = 0;
276 sf->sf_items_updated_prior = 0;
277 sf->sf_items_noscrub = 0;
278 sf->sf_items_igif = 0;
281 static int osd_scrub_file_load(struct osd_scrub *scrub)
284 char *name = LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name;
285 int len = sizeof(scrub->os_file_disk);
288 rc = osd_ldiskfs_read(scrub->os_inode, &scrub->os_file_disk, len, &pos);
290 struct scrub_file *sf = &scrub->os_file;
292 osd_scrub_file_to_cpu(sf, &scrub->os_file_disk);
293 if (sf->sf_magic != SCRUB_MAGIC_V1) {
294 CWARN("%.16s: invalid scrub magic 0x%x != 0x%x\n,",
295 name, sf->sf_magic, SCRUB_MAGIC_V1);
296 /* Process it as new scrub file. */
301 } else if (rc != 0) {
302 CERROR("%.16s: fail to load scrub file, expected = %d, "
303 "rc = %d\n", name, len, rc);
307 /* return -ENOENT for empty scrub file case. */
314 int osd_scrub_file_store(struct osd_scrub *scrub)
316 struct osd_device *dev;
319 int len = sizeof(scrub->os_file_disk);
323 dev = container_of0(scrub, struct osd_device, od_scrub);
324 credits = osd_dto_credits_noquota[DTO_WRITE_BASE] +
325 osd_dto_credits_noquota[DTO_WRITE_BLOCK];
326 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC, credits);
329 CERROR("%.16s: fail to start trans for scrub store, rc = %d\n",
330 LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,rc);
334 osd_scrub_file_to_le(&scrub->os_file_disk, &scrub->os_file);
335 rc = osd_ldiskfs_write_record(scrub->os_inode, &scrub->os_file_disk,
337 ldiskfs_journal_stop(jh);
339 CERROR("%.16s: fail to store scrub file, expected = %d, "
341 LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,
343 scrub->os_time_last_checkpoint = cfs_time_current();
344 scrub->os_time_next_checkpoint = scrub->os_time_last_checkpoint +
345 cfs_time_seconds(SCRUB_CHECKPOINT_INTERVAL);
350 osd_scrub_convert_ff(struct osd_thread_info *info, struct osd_device *dev,
351 struct inode *inode, const struct lu_fid *fid)
353 struct filter_fid_old *ff = &info->oti_ff;
354 struct dentry *dentry = &info->oti_obj_dentry;
358 bool removed = false;
362 if (dev->od_scrub.os_file.sf_param & SP_DRYRUN)
365 /* We want the LMA to fit into the 256-byte OST inode, so operate
367 * 1) read old XATTR_NAME_FID and save the parent FID;
368 * 2) delete the old XATTR_NAME_FID;
369 * 3) make new LMA and add it;
370 * 4) generate new XATTR_NAME_FID with the saved parent FID and add it.
372 * Making the LMA to fit into the 256-byte OST inode can save time for
373 * normal osd_check_lma() and for other OI scrub scanning in future.
374 * So it is worth to make some slow conversion here. */
375 jh = osd_journal_start_sb(osd_sb(dev), LDISKFS_HT_MISC,
376 osd_dto_credits_noquota[DTO_XATTR_SET] * 3);
379 CERROR("%s: fail to start trans for convert ff: "DFID
381 osd_name(dev), PFID(fid), rc);
385 /* 1) read old XATTR_NAME_FID and save the parent FID */
386 rc = __osd_xattr_get(inode, dentry, XATTR_NAME_FID, ff, sizeof(*ff));
387 if (rc == sizeof(*ff)) {
388 /* 2) delete the old XATTR_NAME_FID */
389 ll_vfs_dq_init(inode);
390 rc = inode->i_op->removexattr(dentry, XATTR_NAME_FID);
395 } else if (unlikely(rc == -ENODATA)) {
397 } else if (rc != sizeof(struct filter_fid)) {
398 GOTO(stop, rc = -EINVAL);
401 /* 3) make new LMA and add it */
402 rc = osd_ea_fid_set(info, inode, fid, LMAC_FID_ON_OST, 0);
403 if (rc == 0 && reset)
404 size = sizeof(struct filter_fid);
405 else if (rc != 0 && removed)
406 /* If failed, we should try to add the old back. */
407 size = sizeof(struct filter_fid_old);
409 /* 4) generate new XATTR_NAME_FID with the saved parent FID and add it*/
413 rc1 = __osd_xattr_set(info, inode, XATTR_NAME_FID, ff, size,
415 if (rc1 != 0 && rc == 0)
422 ldiskfs_journal_stop(jh);
427 osd_scrub_check_update(struct osd_thread_info *info, struct osd_device *dev,
428 struct osd_idmap_cache *oic, int val)
430 struct osd_scrub *scrub = &dev->od_scrub;
431 struct scrub_file *sf = &scrub->os_file;
432 struct lu_fid *fid = &oic->oic_fid;
433 struct osd_inode_id *lid = &oic->oic_lid;
434 struct osd_inode_id *lid2 = &info->oti_id;
435 struct osd_inconsistent_item *oii = NULL;
436 struct inode *inode = NULL;
437 int ops = DTO_INDEX_UPDATE;
440 bool converted = false;
443 down_write(&scrub->os_rwsem);
444 scrub->os_new_checked++;
448 if (scrub->os_in_prior)
449 oii = cfs_list_entry(oic, struct osd_inconsistent_item,
452 if (lid->oii_ino < sf->sf_pos_latest_start && oii == NULL)
455 if (fid_is_igif(fid))
458 if (val == SCRUB_NEXT_OSTOBJ_OLD) {
459 inode = osd_iget(info, dev, lid);
462 /* Someone removed the inode. */
463 if (rc == -ENOENT || rc == -ESTALE)
468 sf->sf_flags |= SF_UPGRADE;
469 sf->sf_internal_flags &= ~SIF_NO_HANDLE_OLD_FID;
470 dev->od_check_ff = 1;
471 rc = osd_scrub_convert_ff(info, dev, inode, fid);
478 if ((val == SCRUB_NEXT_NOLMA) &&
479 (!scrub->os_convert_igif || OBD_FAIL_CHECK(OBD_FAIL_FID_NOLMA)))
482 if ((oii != NULL && oii->oii_insert) || (val == SCRUB_NEXT_NOLMA))
485 rc = osd_oi_lookup(info, dev, fid, lid2,
486 (val == SCRUB_NEXT_OSTOBJ ||
487 val == SCRUB_NEXT_OSTOBJ_OLD) ? OI_KNOWN_ON_OST : 0);
494 inode = osd_iget(info, dev, lid);
497 /* Someone removed the inode. */
498 if (rc == -ENOENT || rc == -ESTALE)
504 scrub->os_full_speed = 1;
505 ops = DTO_INDEX_INSERT;
506 idx = osd_oi_fid2idx(dev, fid);
508 case SCRUB_NEXT_NOLMA:
509 sf->sf_flags |= SF_UPGRADE;
510 if (!(sf->sf_param & SP_DRYRUN)) {
511 rc = osd_ea_fid_set(info, inode, fid, 0, 0);
516 if (!(sf->sf_flags & SF_INCONSISTENT))
517 dev->od_igif_inoi = 0;
519 case SCRUB_NEXT_OSTOBJ:
520 sf->sf_flags |= SF_INCONSISTENT;
521 case SCRUB_NEXT_OSTOBJ_OLD:
524 sf->sf_flags |= SF_RECREATED;
525 if (unlikely(!ldiskfs_test_bit(idx, sf->sf_oi_bitmap)))
526 ldiskfs_set_bit(idx, sf->sf_oi_bitmap);
529 } else if (osd_id_eq(lid, lid2)) {
531 sf->sf_items_updated++;
535 scrub->os_full_speed = 1;
536 sf->sf_flags |= SF_INCONSISTENT;
538 /* XXX: If the device is restored from file-level backup, then
539 * some IGIFs may have been already in OI files, and some
540 * may be not yet. Means upgrading from 1.8 may be partly
541 * processed, but some clients may hold some immobilized
542 * IGIFs, and use them to access related objects. Under
543 * such case, OSD does not know whether an given IGIF has
544 * been processed or to be processed, and it also cannot
545 * generate local ino#/gen# directly from the immobilized
546 * IGIF because of the backup/restore. Then force OSD to
547 * lookup the given IGIF in OI files, and if no entry,
548 * then ask the client to retry after upgrading completed.
549 * No better choice. */
550 dev->od_igif_inoi = 1;
553 rc = osd_scrub_refresh_mapping(info, dev, fid, lid, ops, false,
554 (val == SCRUB_NEXT_OSTOBJ ||
555 val == SCRUB_NEXT_OSTOBJ_OLD) ? OI_KNOWN_ON_OST : 0);
557 if (scrub->os_in_prior)
558 sf->sf_items_updated_prior++;
560 sf->sf_items_updated++;
562 /* The target has been changed, need to be re-loaded. */
563 lu_object_purge(info->oti_env, osd2lu_dev(dev), fid);
570 sf->sf_items_failed++;
571 if (sf->sf_pos_first_inconsistent == 0 ||
572 sf->sf_pos_first_inconsistent > lid->oii_ino)
573 sf->sf_pos_first_inconsistent = lid->oii_ino;
578 /* There may be conflict unlink during the OI scrub,
579 * if happend, then remove the new added OI mapping. */
580 if (ops == DTO_INDEX_INSERT && inode != NULL && !IS_ERR(inode) &&
581 unlikely(inode->i_nlink == 0))
582 osd_scrub_refresh_mapping(info, dev, fid, lid,
583 DTO_INDEX_DELETE, false,
584 (val == SCRUB_NEXT_OSTOBJ ||
585 val == SCRUB_NEXT_OSTOBJ_OLD) ?
586 OI_KNOWN_ON_OST : 0);
587 up_write(&scrub->os_rwsem);
589 if (inode != NULL && !IS_ERR(inode))
593 LASSERT(!cfs_list_empty(&oii->oii_list));
595 spin_lock(&scrub->os_lock);
596 cfs_list_del_init(&oii->oii_list);
597 spin_unlock(&scrub->os_lock);
600 RETURN(sf->sf_param & SP_FAILOUT ? rc : 0);
605 static int osd_scrub_prep(struct osd_device *dev)
607 struct osd_scrub *scrub = &dev->od_scrub;
608 struct ptlrpc_thread *thread = &scrub->os_thread;
609 struct scrub_file *sf = &scrub->os_file;
610 __u32 flags = scrub->os_start_flags;
612 bool drop_dryrun = false;
615 down_write(&scrub->os_rwsem);
616 if (flags & SS_SET_FAILOUT)
617 sf->sf_param |= SP_FAILOUT;
619 if (flags & SS_CLEAR_FAILOUT)
620 sf->sf_param &= ~SP_FAILOUT;
622 if (flags & SS_SET_DRYRUN)
623 sf->sf_param |= SP_DRYRUN;
625 if (flags & SS_CLEAR_DRYRUN && sf->sf_param & SP_DRYRUN) {
626 sf->sf_param &= ~SP_DRYRUN;
630 if (flags & SS_RESET)
631 osd_scrub_file_reset(scrub,
632 LDISKFS_SB(osd_sb(dev))->s_es->s_uuid, 0);
634 if (flags & SS_AUTO) {
635 scrub->os_full_speed = 1;
636 sf->sf_flags |= SF_AUTO;
637 /* For the case of OI scrub auto triggered, NOT dryrun. */
638 sf->sf_param &= ~SP_FAILOUT;
640 scrub->os_full_speed = 0;
643 if (sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT | SF_UPGRADE))
644 scrub->os_full_speed = 1;
646 scrub->os_in_prior = 0;
647 spin_lock(&scrub->os_lock);
648 scrub->os_waiting = 0;
649 scrub->os_paused = 0;
650 spin_unlock(&scrub->os_lock);
651 scrub->os_new_checked = 0;
652 if (drop_dryrun && sf->sf_pos_first_inconsistent != 0)
653 sf->sf_pos_latest_start = sf->sf_pos_first_inconsistent;
654 else if (sf->sf_pos_last_checkpoint != 0)
655 sf->sf_pos_latest_start = sf->sf_pos_last_checkpoint + 1;
657 sf->sf_pos_latest_start = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
659 scrub->os_pos_current = sf->sf_pos_latest_start;
660 sf->sf_status = SS_SCANNING;
661 sf->sf_time_latest_start = cfs_time_current_sec();
662 sf->sf_time_last_checkpoint = sf->sf_time_latest_start;
663 rc = osd_scrub_file_store(scrub);
665 spin_lock(&scrub->os_lock);
666 thread_set_flags(thread, SVC_RUNNING);
667 spin_unlock(&scrub->os_lock);
668 wake_up_all(&thread->t_ctl_waitq);
670 up_write(&scrub->os_rwsem);
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 if (!(sf->sf_param & SP_DRYRUN)) {
721 memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
722 sf->sf_flags &= ~(SF_RECREATED | SF_INCONSISTENT |
723 SF_UPGRADE | SF_AUTO);
725 sf->sf_time_last_complete = sf->sf_time_last_checkpoint;
726 sf->sf_success_count++;
727 } else if (result == 0) {
728 if (scrub->os_paused)
729 sf->sf_status = SS_PAUSED;
731 sf->sf_status = SS_STOPPED;
733 sf->sf_status = SS_FAILED;
735 sf->sf_run_time += cfs_duration_sec(cfs_time_current() + HALF_SEC -
736 scrub->os_time_last_checkpoint);
737 result = osd_scrub_file_store(scrub);
739 CERROR("%.16s: fail to osd_scrub_post, rc = %d\n",
740 LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,
742 up_write(&scrub->os_rwsem);
747 /* iteration engine */
749 struct osd_iit_param {
750 struct super_block *sb;
751 struct buffer_head *bitmap;
757 typedef int (*osd_iit_next_policy)(struct osd_thread_info *info,
758 struct osd_device *dev,
759 struct osd_iit_param *param,
760 struct osd_idmap_cache **oic,
763 typedef int (*osd_iit_exec_policy)(struct osd_thread_info *info,
764 struct osd_device *dev,
765 struct osd_iit_param *param,
766 struct osd_idmap_cache *oic,
767 int *noslot, int rc);
769 static int osd_iit_next(struct osd_iit_param *param, __u32 *pos)
771 param->offset = ldiskfs_find_next_bit(param->bitmap->b_data,
772 LDISKFS_INODES_PER_GROUP(param->sb), param->offset);
773 if (param->offset >= LDISKFS_INODES_PER_GROUP(param->sb)) {
774 *pos = 1 + (param->bg+1) * LDISKFS_INODES_PER_GROUP(param->sb);
775 return SCRUB_NEXT_BREAK;
777 *pos = param->gbase + param->offset;
783 * \retval SCRUB_NEXT_OSTOBJ_OLD: FID-on-OST
784 * \retval 0: FID-on-MDT
786 static int osd_scrub_check_local_fldb(struct osd_thread_info *info,
787 struct osd_device *dev,
790 /* XXX: The initial OI scrub will scan the top level /O to generate
791 * a small local FLDB according to the <seq>. If the given FID
792 * is in the local FLDB, then it is FID-on-OST; otherwise it's
793 * quite possible for FID-on-MDT. */
795 return SCRUB_NEXT_OSTOBJ_OLD;
800 static int osd_scrub_get_fid(struct osd_thread_info *info,
801 struct osd_device *dev, struct inode *inode,
802 struct lu_fid *fid, bool scrub)
804 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
806 bool has_lma = false;
808 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
811 if (lma->lma_compat & LMAC_NOT_IN_OI ||
812 lma->lma_incompat & LMAI_AGENT)
813 return SCRUB_NEXT_CONTINUE;
815 *fid = lma->lma_self_fid;
819 if (lma->lma_compat & LMAC_FID_ON_OST)
820 return SCRUB_NEXT_OSTOBJ;
822 if (fid_is_idif(fid))
823 return SCRUB_NEXT_OSTOBJ_OLD;
825 /* For local object. */
826 if (fid_is_internal(fid))
829 /* For external visible MDT-object with non-normal FID. */
830 if (fid_is_namespace_visible(fid) && !fid_is_norm(fid))
833 /* For the object with normal FID, it may be MDT-object,
834 * or may be 2.4 OST-object, need further distinguish.
835 * Fall through to next section. */
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);
910 ldiskfs_test_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB)) {
911 /* Only skip it for the first OI scrub accessing. */
912 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
913 GOTO(put, rc = SCRUB_NEXT_NOSCRUB);
916 rc = osd_scrub_get_fid(info, dev, inode, fid, scrub);
925 static int osd_scrub_next(struct osd_thread_info *info, struct osd_device *dev,
926 struct osd_iit_param *param,
927 struct osd_idmap_cache **oic, int noslot)
929 struct osd_scrub *scrub = &dev->od_scrub;
930 struct ptlrpc_thread *thread = &scrub->os_thread;
932 struct osd_inode_id *lid;
935 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_DELAY) && cfs_fail_val > 0) {
936 struct l_wait_info lwi;
938 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val), NULL, NULL);
939 l_wait_event(thread->t_ctl_waitq,
940 !cfs_list_empty(&scrub->os_inconsistent_items) ||
941 !thread_is_running(thread),
945 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_CRASH)) {
946 spin_lock(&scrub->os_lock);
947 thread_set_flags(thread, SVC_STOPPING);
948 spin_unlock(&scrub->os_lock);
949 return SCRUB_NEXT_CRASH;
952 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_FATAL))
953 return SCRUB_NEXT_FATAL;
955 if (unlikely(!thread_is_running(thread)))
956 return SCRUB_NEXT_EXIT;
958 if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
959 struct osd_inconsistent_item *oii;
961 oii = cfs_list_entry(scrub->os_inconsistent_items.next,
962 struct osd_inconsistent_item, oii_list);
963 *oic = &oii->oii_cache;
964 scrub->os_in_prior = 1;
969 return SCRUB_NEXT_WAIT;
971 rc = osd_iit_next(param, &scrub->os_pos_current);
975 *oic = &scrub->os_oic;
976 fid = &(*oic)->oic_fid;
977 lid = &(*oic)->oic_lid;
978 rc = osd_iit_iget(info, dev, fid, lid,
979 scrub->os_pos_current, param->sb, true);
983 static int osd_preload_next(struct osd_thread_info *info,
984 struct osd_device *dev, struct osd_iit_param *param,
985 struct osd_idmap_cache **oic, int noslot)
987 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
988 struct osd_scrub *scrub;
989 struct ptlrpc_thread *thread;
992 rc = osd_iit_next(param, &ooc->ooc_pos_preload);
996 scrub = &dev->od_scrub;
997 thread = &scrub->os_thread;
998 if (thread_is_running(thread) &&
999 ooc->ooc_pos_preload >= scrub->os_pos_current)
1000 return SCRUB_NEXT_EXIT;
1002 rc = osd_iit_iget(info, dev,
1003 &ooc->ooc_cache[ooc->ooc_producer_idx].oic_fid,
1004 &ooc->ooc_cache[ooc->ooc_producer_idx].oic_lid,
1005 ooc->ooc_pos_preload, param->sb, false);
1006 /* If succeed, it needs to move forward; otherwise up layer LFSCK may
1007 * ignore the failure, so it still need to skip the inode next time. */
1008 ooc->ooc_pos_preload = param->gbase + ++(param->offset);
1013 osd_scrub_wakeup(struct osd_scrub *scrub, struct osd_otable_it *it)
1015 spin_lock(&scrub->os_lock);
1016 if (osd_scrub_has_window(scrub, &it->ooi_cache) ||
1017 !cfs_list_empty(&scrub->os_inconsistent_items) ||
1018 it->ooi_waiting || !thread_is_running(&scrub->os_thread))
1019 scrub->os_waiting = 0;
1021 scrub->os_waiting = 1;
1022 spin_unlock(&scrub->os_lock);
1024 return !scrub->os_waiting;
1027 static int osd_scrub_exec(struct osd_thread_info *info, struct osd_device *dev,
1028 struct osd_iit_param *param,
1029 struct osd_idmap_cache *oic, int *noslot, int rc)
1031 struct l_wait_info lwi = { 0 };
1032 struct osd_scrub *scrub = &dev->od_scrub;
1033 struct scrub_file *sf = &scrub->os_file;
1034 struct ptlrpc_thread *thread = &scrub->os_thread;
1035 struct osd_otable_it *it = dev->od_otable_it;
1036 struct osd_otable_cache *ooc = it ? &it->ooi_cache : NULL;
1039 case SCRUB_NEXT_CONTINUE:
1041 case SCRUB_NEXT_WAIT:
1043 case SCRUB_NEXT_NOSCRUB:
1044 down_write(&scrub->os_rwsem);
1045 scrub->os_new_checked++;
1046 sf->sf_items_noscrub++;
1047 up_write(&scrub->os_rwsem);
1051 rc = osd_scrub_check_update(info, dev, oic, rc);
1055 rc = osd_scrub_checkpoint(scrub);
1057 CERROR("%.16s: fail to checkpoint, pos = %u, rc = %d\n",
1058 LDISKFS_SB(param->sb)->s_es->s_volume_name,
1059 scrub->os_pos_current, rc);
1060 /* Continue, as long as the scrub itself can go ahead. */
1063 if (scrub->os_in_prior) {
1064 scrub->os_in_prior = 0;
1069 scrub->os_pos_current = param->gbase + ++(param->offset);
1072 if (it != NULL && it->ooi_waiting && ooc != NULL &&
1073 ooc->ooc_pos_preload < scrub->os_pos_current) {
1074 spin_lock(&scrub->os_lock);
1075 it->ooi_waiting = 0;
1076 wake_up_all(&thread->t_ctl_waitq);
1077 spin_unlock(&scrub->os_lock);
1080 if (scrub->os_full_speed || rc == SCRUB_NEXT_CONTINUE)
1083 if (osd_scrub_has_window(scrub, ooc)) {
1088 l_wait_event(thread->t_ctl_waitq,
1089 osd_scrub_wakeup(scrub, it),
1092 if (osd_scrub_has_window(scrub, ooc))
1099 static int osd_preload_exec(struct osd_thread_info *info,
1100 struct osd_device *dev, struct osd_iit_param *param,
1101 struct osd_idmap_cache *oic, int *noslot, int rc)
1103 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
1106 ooc->ooc_cached_items++;
1107 ooc->ooc_producer_idx = (ooc->ooc_producer_idx + 1) &
1108 ~OSD_OTABLE_IT_CACHE_MASK;
1110 return rc > 0 ? 0 : rc;
1113 #define SCRUB_IT_ALL 1
1114 #define SCRUB_IT_CRASH 2
1116 static int osd_inode_iteration(struct osd_thread_info *info,
1117 struct osd_device *dev, __u32 max, bool preload)
1119 osd_iit_next_policy next;
1120 osd_iit_exec_policy exec;
1123 struct osd_iit_param param;
1130 struct osd_scrub *scrub = &dev->od_scrub;
1132 next = osd_scrub_next;
1133 exec = osd_scrub_exec;
1134 pos = &scrub->os_pos_current;
1135 count = &scrub->os_new_checked;
1137 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
1139 next = osd_preload_next;
1140 exec = osd_preload_exec;
1141 pos = &ooc->ooc_pos_preload;
1142 count = &ooc->ooc_cached_items;
1144 param.sb = osd_sb(dev);
1145 limit = le32_to_cpu(LDISKFS_SB(param.sb)->s_es->s_inodes_count);
1147 while (*pos <= limit && *count < max) {
1148 struct osd_idmap_cache *oic = NULL;
1150 param.bg = (*pos - 1) / LDISKFS_INODES_PER_GROUP(param.sb);
1151 param.offset = (*pos - 1) % LDISKFS_INODES_PER_GROUP(param.sb);
1152 param.gbase = 1 + param.bg * LDISKFS_INODES_PER_GROUP(param.sb);
1153 param.bitmap = ldiskfs_read_inode_bitmap(param.sb, param.bg);
1154 if (param.bitmap == NULL) {
1155 CERROR("%.16s: fail to read bitmap for %u, "
1156 "scrub will stop, urgent mode\n",
1157 LDISKFS_SB(param.sb)->s_es->s_volume_name,
1162 while (param.offset < LDISKFS_INODES_PER_GROUP(param.sb) &&
1164 rc = next(info, dev, ¶m, &oic, noslot);
1166 case SCRUB_NEXT_BREAK:
1168 case SCRUB_NEXT_EXIT:
1169 brelse(param.bitmap);
1171 case SCRUB_NEXT_CRASH:
1172 brelse(param.bitmap);
1173 RETURN(SCRUB_IT_CRASH);
1174 case SCRUB_NEXT_FATAL:
1175 brelse(param.bitmap);
1179 rc = exec(info, dev, ¶m, oic, &noslot, rc);
1181 brelse(param.bitmap);
1187 brelse(param.bitmap);
1191 RETURN(SCRUB_IT_ALL);
1195 static int osd_otable_it_preload(const struct lu_env *env,
1196 struct osd_otable_it *it)
1198 struct osd_device *dev = it->ooi_dev;
1199 struct osd_scrub *scrub = &dev->od_scrub;
1200 struct osd_otable_cache *ooc = &it->ooi_cache;
1204 rc = osd_inode_iteration(osd_oti_get(env), dev,
1205 OSD_OTABLE_IT_CACHE_SIZE, true);
1206 if (rc == SCRUB_IT_ALL)
1207 it->ooi_all_cached = 1;
1209 CDEBUG(D_LFSCK, "OSD pre-loaded: max = %u, preload = %u, rc = %d\n",
1210 le32_to_cpu(LDISKFS_SB(osd_sb(dev))->s_es->s_inodes_count),
1211 ooc->ooc_pos_preload, rc);
1213 if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
1214 spin_lock(&scrub->os_lock);
1215 scrub->os_waiting = 0;
1216 wake_up_all(&scrub->os_thread.t_ctl_waitq);
1217 spin_unlock(&scrub->os_lock);
1220 RETURN(rc < 0 ? rc : ooc->ooc_cached_items);
1223 static int osd_scrub_main(void *args)
1226 struct osd_device *dev = (struct osd_device *)args;
1227 struct osd_scrub *scrub = &dev->od_scrub;
1228 struct ptlrpc_thread *thread = &scrub->os_thread;
1229 struct super_block *sb = osd_sb(dev);
1233 rc = lu_env_init(&env, LCT_LOCAL);
1235 CERROR("%.16s: OI scrub, fail to init env, rc = %d\n",
1236 LDISKFS_SB(sb)->s_es->s_volume_name, rc);
1240 rc = osd_scrub_prep(dev);
1242 CERROR("%.16s: OI scrub, fail to scrub prep, rc = %d\n",
1243 LDISKFS_SB(sb)->s_es->s_volume_name, rc);
1247 if (!scrub->os_full_speed) {
1248 struct l_wait_info lwi = { 0 };
1249 struct osd_otable_it *it = dev->od_otable_it;
1250 struct osd_otable_cache *ooc = &it->ooi_cache;
1252 l_wait_event(thread->t_ctl_waitq,
1253 it->ooi_user_ready || !thread_is_running(thread),
1255 if (unlikely(!thread_is_running(thread)))
1258 scrub->os_pos_current = ooc->ooc_pos_preload;
1261 CDEBUG(D_LFSCK, "OI scrub: flags = 0x%x, pos = %u\n",
1262 scrub->os_start_flags, scrub->os_pos_current);
1264 rc = osd_inode_iteration(osd_oti_get(&env), dev, ~0U, false);
1265 if (unlikely(rc == SCRUB_IT_CRASH))
1266 GOTO(out, rc = -EINVAL);
1270 osd_scrub_post(scrub, rc);
1271 CDEBUG(D_LFSCK, "OI scrub: stop, rc = %d, pos = %u\n",
1272 rc, scrub->os_pos_current);
1275 while (!cfs_list_empty(&scrub->os_inconsistent_items)) {
1276 struct osd_inconsistent_item *oii;
1278 oii = cfs_list_entry(scrub->os_inconsistent_items.next,
1279 struct osd_inconsistent_item, oii_list);
1280 cfs_list_del_init(&oii->oii_list);
1286 spin_lock(&scrub->os_lock);
1287 thread_set_flags(thread, SVC_STOPPED);
1288 wake_up_all(&thread->t_ctl_waitq);
1289 spin_unlock(&scrub->os_lock);
1293 /* initial OI scrub */
1295 typedef int (*scandir_t)(struct osd_thread_info *, struct osd_device *,
1296 struct dentry *, filldir_t filldir);
1298 static int osd_ios_varfid_fill(void *buf, const char *name, int namelen,
1299 loff_t offset, __u64 ino, unsigned d_type);
1300 static int osd_ios_lf_fill(void *buf, const char *name, int namelen,
1301 loff_t offset, __u64 ino, unsigned d_type);
1302 static int osd_ios_dl_fill(void *buf, const char *name, int namelen,
1303 loff_t offset, __u64 ino, unsigned d_type);
1306 osd_ios_general_scan(struct osd_thread_info *info, struct osd_device *dev,
1307 struct dentry *dentry, filldir_t filldir);
1309 osd_ios_ROOT_scan(struct osd_thread_info *info, struct osd_device *dev,
1310 struct dentry *dentry, filldir_t filldir);
1313 osd_ios_OBJECTS_scan(struct osd_thread_info *info, struct osd_device *dev,
1314 struct dentry *dentry, filldir_t filldir);
1317 OLF_SCAN_SUBITEMS = 0x0001,
1318 OLF_HIDE_FID = 0x0002,
1319 OLF_SHOW_NAME = 0x0004,
1325 struct lu_fid olm_fid;
1327 scandir_t olm_scandir;
1328 filldir_t olm_filldir;
1331 /* Add the new introduced local files in the list in the future. */
1332 static const struct osd_lf_map osd_lf_maps[] = {
1334 { CATLIST, { FID_SEQ_LOCAL_FILE, LLOG_CATALOGS_OID, 0 }, OLF_SHOW_NAME,
1338 { MOUNT_CONFIGS_DIR, { FID_SEQ_LOCAL_FILE, MGS_CONFIGS_OID, 0 },
1339 OLF_SCAN_SUBITEMS, osd_ios_general_scan,
1340 osd_ios_varfid_fill },
1342 /* NIDTBL_VERSIONS */
1343 { MGS_NIDTBL_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1344 osd_ios_general_scan, osd_ios_varfid_fill },
1347 { "PENDING", { 0, 0, 0 }, 0, NULL, NULL },
1350 { "ROOT", { FID_SEQ_ROOT, 1, 0 },
1351 OLF_SCAN_SUBITEMS | OLF_HIDE_FID, osd_ios_ROOT_scan, NULL },
1353 /* changelog_catalog */
1354 { CHANGELOG_CATALOG, { 0, 0, 0 }, 0, NULL, NULL },
1356 /* changelog_users */
1357 { CHANGELOG_USERS, { 0, 0, 0 }, 0, NULL, NULL },
1360 { "fld", { FID_SEQ_LOCAL_FILE, FLD_INDEX_OID, 0 }, OLF_SHOW_NAME,
1364 { LAST_RCVD, { FID_SEQ_LOCAL_FILE, LAST_RECV_OID, 0 }, OLF_SHOW_NAME,
1367 /* lfsck_bookmark */
1368 { "lfsck_bookmark", { 0, 0, 0 }, 0, NULL, NULL },
1371 { LOV_OBJID, { FID_SEQ_LOCAL_FILE, MDD_LOV_OBJ_OID, 0 }, OLF_SHOW_NAME,
1375 { LOV_OBJSEQ, { FID_SEQ_LOCAL_FILE, MDD_LOV_OBJ_OSEQ, 0 },
1376 OLF_SHOW_NAME, NULL, NULL },
1379 { QMT_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1380 osd_ios_general_scan, osd_ios_varfid_fill },
1383 { QSD_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1384 osd_ios_general_scan, osd_ios_varfid_fill },
1387 { "seq_ctl", { FID_SEQ_LOCAL_FILE, FID_SEQ_CTL_OID, 0 },
1388 OLF_SHOW_NAME, NULL, NULL },
1391 { "seq_srv", { FID_SEQ_LOCAL_FILE, FID_SEQ_SRV_OID, 0 },
1392 OLF_SHOW_NAME, NULL, NULL },
1395 { HEALTH_CHECK, { FID_SEQ_LOCAL_FILE, OFD_HEALTH_CHECK_OID, 0 },
1396 OLF_SHOW_NAME, NULL, NULL },
1398 /* lfsck_namespace */
1399 { "lfsck_namespace", { 0, 0, 0 }, 0, NULL, NULL },
1401 /* OBJECTS, upgrade from old device */
1402 { OBJECTS, { 0, 0, 0 }, OLF_SCAN_SUBITEMS, osd_ios_OBJECTS_scan, NULL },
1404 /* lquota_v2.user, upgrade from old device */
1405 { "lquota_v2.user", { 0, 0, 0 }, 0, NULL, NULL },
1407 /* lquota_v2.group, upgrade from old device */
1408 { "lquota_v2.group", { 0, 0, 0 }, 0, NULL, NULL },
1410 /* LAST_GROUP, upgrade from old device */
1411 { "LAST_GROUP", { FID_SEQ_LOCAL_FILE, OFD_LAST_GROUP_OID, 0 },
1412 OLF_SHOW_NAME, NULL, NULL },
1414 /* SLAVE_LOG, llog for destroy slave stripes of striped dir */
1415 { "SLAVE_LOG", { FID_SEQ_LOCAL_FILE, SLAVE_LLOG_CATALOGS_OID, 0 },
1416 OLF_SHOW_NAME, NULL, NULL },
1419 { "lost+found", { 0, 0, 0 }, OLF_SCAN_SUBITEMS | OLF_NO_OI,
1420 osd_ios_general_scan, osd_ios_lf_fill },
1422 { NULL, { 0, 0, 0 }, 0, NULL, NULL }
1425 /* Add the new introduced files under .lustre/ in the list in the future. */
1426 static const struct osd_lf_map osd_dl_maps[] = {
1428 { "fid", { FID_SEQ_DOT_LUSTRE, FID_OID_DOT_LUSTRE_OBF, 0 }, 0,
1431 /* .lustre/lost+found */
1432 { "lost+found", { FID_SEQ_DOT_LUSTRE, FID_OID_DOT_LUSTRE_LPF, 0 }, 0,
1435 { NULL, { 0, 0, 0 }, 0, NULL, NULL }
1438 struct osd_ios_item {
1439 cfs_list_t oii_list;
1440 struct dentry *oii_dentry;
1441 scandir_t oii_scandir;
1442 filldir_t oii_filldir;
1445 struct osd_ios_filldir_buf {
1446 #ifdef HAVE_DIR_CONTEXT
1447 /* please keep it as first member */
1448 struct dir_context ctx;
1450 struct osd_thread_info *oifb_info;
1451 struct osd_device *oifb_dev;
1452 struct dentry *oifb_dentry;
1455 static inline struct dentry *
1456 osd_ios_lookup_one_len(const char *name, struct dentry *parent, int namelen)
1458 struct dentry *dentry;
1460 CDEBUG(D_LFSCK, "init lookup one: parent = %.*s, name = %.*s\n",
1461 parent->d_name.len, parent->d_name.name, namelen, name);
1463 dentry = ll_lookup_one_len(name, parent, namelen);
1464 if (!IS_ERR(dentry) && dentry->d_inode == NULL) {
1466 return ERR_PTR(-ENOENT);
1473 osd_ios_new_item(struct osd_device *dev, struct dentry *dentry,
1474 scandir_t scandir, filldir_t filldir)
1476 struct osd_ios_item *item;
1479 OBD_ALLOC_PTR(item);
1483 CFS_INIT_LIST_HEAD(&item->oii_list);
1484 item->oii_dentry = dget(dentry);
1485 item->oii_scandir = scandir;
1486 item->oii_filldir = filldir;
1487 cfs_list_add_tail(&item->oii_list, &dev->od_ios_list);
1493 * osd_ios_scan_one() - check/fix LMA FID and OI entry for one inode
1495 * The passed \a inode's \a fid is verified against the LMA FID. If the \a fid
1496 * is NULL or is empty the IGIF FID is used. The FID is verified in the OI to
1497 * reference the inode, or fixed if it is missing or references another inode.
1500 osd_ios_scan_one(struct osd_thread_info *info, struct osd_device *dev,
1501 struct inode *inode, const struct lu_fid *fid, int flags)
1503 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
1504 struct osd_inode_id *id = &info->oti_id;
1505 struct osd_inode_id *id2 = &info->oti_id2;
1506 struct osd_scrub *scrub = &dev->od_scrub;
1507 struct scrub_file *sf = &scrub->os_file;
1512 CDEBUG(D_LFSCK, "init scan one: ino = %ld\n", inode->i_ino);
1514 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
1515 if (rc != 0 && rc != -ENODATA)
1518 osd_id_gen(id, inode->i_ino, inode->i_generation);
1519 if (rc == -ENODATA) {
1520 if (fid == NULL || fid_is_zero(fid) || flags & OLF_HIDE_FID)
1521 lu_igif_build(&tfid, inode->i_ino, inode->i_generation);
1524 rc = osd_ea_fid_set(info, inode, &tfid, 0, 0);
1528 if (lma->lma_compat & LMAC_NOT_IN_OI)
1531 tfid = lma->lma_self_fid;
1534 rc = osd_oi_lookup(info, dev, &tfid, id2, 0);
1539 rc = osd_scrub_refresh_mapping(info, dev, &tfid, id,
1540 DTO_INDEX_INSERT, true, 0);
1547 if (osd_id_eq_strict(id, id2))
1550 if (!(sf->sf_flags & SF_INCONSISTENT)) {
1551 osd_scrub_file_reset(scrub,
1552 LDISKFS_SB(osd_sb(dev))->s_es->s_uuid,
1554 rc = osd_scrub_file_store(scrub);
1559 rc = osd_scrub_refresh_mapping(info, dev, &tfid, id,
1560 DTO_INDEX_UPDATE, true, 0);
1568 * It scans the /lost+found, and for the OST-object (with filter_fid
1569 * or filter_fid_old), move them back to its proper /O/<seq>/d<x>.
1571 static int osd_ios_lf_fill(void *buf, const char *name, int namelen,
1572 loff_t offset, __u64 ino, unsigned d_type)
1574 struct osd_ios_filldir_buf *fill_buf = buf;
1575 struct osd_thread_info *info = fill_buf->oifb_info;
1576 struct osd_device *dev = fill_buf->oifb_dev;
1577 struct lu_fid *fid = &info->oti_fid;
1578 struct osd_scrub *scrub = &dev->od_scrub;
1579 struct dentry *parent = fill_buf->oifb_dentry;
1580 struct dentry *child;
1581 struct inode *dir = parent->d_inode;
1582 struct inode *inode;
1586 /* skip any '.' started names */
1590 scrub->os_lf_scanned++;
1591 child = osd_ios_lookup_one_len(name, parent, namelen);
1592 if (IS_ERR(child)) {
1593 CWARN("%s: cannot lookup child '%.*s': rc = %d\n",
1594 osd_name(dev), namelen, name, (int)PTR_ERR(child));
1598 inode = child->d_inode;
1599 if (S_ISDIR(inode->i_mode)) {
1600 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
1603 CWARN("%s: cannot add child '%.*s': rc = %d\n",
1604 osd_name(dev), namelen, name, rc);
1608 if (!S_ISREG(inode->i_mode))
1611 rc = osd_scrub_get_fid(info, dev, inode, fid, true);
1612 if (rc == SCRUB_NEXT_OSTOBJ || rc == SCRUB_NEXT_OSTOBJ_OLD) {
1613 rc = osd_obj_map_recover(info, dev, dir, child, fid);
1615 CDEBUG(D_LFSCK, "recovered '%.*s' ["DFID"] from "
1616 "/lost+found.\n", namelen, name, PFID(fid));
1617 scrub->os_lf_repaired++;
1619 CWARN("%s: cannot rename for '%.*s' "DFID": rc = %d\n",
1620 osd_name(dev), namelen, name, PFID(fid), rc);
1624 /* XXX: For MDT-objects, we can move them from /lost+found to namespace
1625 * visible place, such as the /ROOT/.lustre/lost+found, then LFSCK
1626 * can process them in furtuer. */
1632 scrub->os_lf_failed++;
1634 /* skip the failure to make the scanning to continue. */
1638 static int osd_ios_varfid_fill(void *buf, const char *name, int namelen,
1639 loff_t offset, __u64 ino, unsigned d_type)
1641 struct osd_ios_filldir_buf *fill_buf = buf;
1642 struct osd_device *dev = fill_buf->oifb_dev;
1643 struct dentry *child;
1647 /* skip any '.' started names */
1651 child = osd_ios_lookup_one_len(name, fill_buf->oifb_dentry, namelen);
1653 RETURN(PTR_ERR(child));
1655 rc = osd_ios_scan_one(fill_buf->oifb_info, dev, child->d_inode,
1657 if (rc == 0 && S_ISDIR(child->d_inode->i_mode))
1658 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
1659 osd_ios_varfid_fill);
1665 static int osd_ios_dl_fill(void *buf, const char *name, int namelen,
1666 loff_t offset, __u64 ino, unsigned d_type)
1668 struct osd_ios_filldir_buf *fill_buf = buf;
1669 struct osd_device *dev = fill_buf->oifb_dev;
1670 const struct osd_lf_map *map;
1671 struct dentry *child;
1675 /* skip any '.' started names */
1679 for (map = osd_dl_maps; map->olm_name != NULL; map++) {
1680 if (strlen(map->olm_name) != namelen)
1683 if (strncmp(map->olm_name, name, namelen) == 0)
1687 if (map->olm_name == NULL)
1690 child = osd_ios_lookup_one_len(name, fill_buf->oifb_dentry, namelen);
1692 RETURN(PTR_ERR(child));
1694 rc = osd_ios_scan_one(fill_buf->oifb_info, dev, child->d_inode,
1695 &map->olm_fid, map->olm_flags);
1701 static int osd_ios_root_fill(void *buf, const char *name, int namelen,
1702 loff_t offset, __u64 ino, unsigned d_type)
1704 struct osd_ios_filldir_buf *fill_buf = buf;
1705 struct osd_device *dev = fill_buf->oifb_dev;
1706 const struct osd_lf_map *map;
1707 struct dentry *child;
1711 /* skip any '.' started names */
1715 for (map = osd_lf_maps; map->olm_name != NULL; map++) {
1716 if (strlen(map->olm_name) != namelen)
1719 if (strncmp(map->olm_name, name, namelen) == 0)
1723 if (map->olm_name == NULL)
1726 child = osd_ios_lookup_one_len(name, fill_buf->oifb_dentry, namelen);
1728 RETURN(PTR_ERR(child));
1730 if (!(map->olm_flags & OLF_NO_OI))
1731 rc = osd_ios_scan_one(fill_buf->oifb_info, dev, child->d_inode,
1732 &map->olm_fid, map->olm_flags);
1733 if (rc == 0 && map->olm_flags & OLF_SCAN_SUBITEMS)
1734 rc = osd_ios_new_item(dev, child, map->olm_scandir,
1742 osd_ios_general_scan(struct osd_thread_info *info, struct osd_device *dev,
1743 struct dentry *dentry, filldir_t filldir)
1745 struct osd_ios_filldir_buf buf = {
1746 #ifdef HAVE_DIR_CONTEXT
1747 .ctx.actor = filldir,
1751 .oifb_dentry = dentry };
1752 struct file *filp = &info->oti_it_ea.oie_file;
1753 struct inode *inode = dentry->d_inode;
1754 const struct file_operations *fops = inode->i_fop;
1758 LASSERT(filldir != NULL);
1761 filp->f_dentry = dentry;
1762 filp->f_mode = FMODE_64BITHASH;
1763 filp->f_mapping = inode->i_mapping;
1765 filp->private_data = NULL;
1766 set_file_inode(filp, inode);
1768 #ifdef HAVE_DIR_CONTEXT
1769 buf.ctx.pos = filp->f_pos;
1770 rc = fops->iterate(filp, &buf.ctx);
1771 filp->f_pos = buf.ctx.pos;
1773 rc = fops->readdir(filp, &buf, filldir);
1775 fops->release(inode, filp);
1781 osd_ios_ROOT_scan(struct osd_thread_info *info, struct osd_device *dev,
1782 struct dentry *dentry, filldir_t filldir)
1784 struct osd_scrub *scrub = &dev->od_scrub;
1785 struct scrub_file *sf = &scrub->os_file;
1786 struct dentry *child;
1790 /* It is existing MDT0 device. We only allow the case of object without
1791 * LMA to happen on the MDT0, which is usually for old 1.8 MDT. Then we
1792 * can generate IGIF mode FID for the object and related OI mapping. If
1793 * it is on other MDTs, then becuase file-level backup/restore, related
1794 * OI mapping may be invalid already, we do not know which is the right
1795 * FID for the object. We only allow IGIF objects to reside on the MDT0.
1797 * XXX: For the case of object on non-MDT0 device with neither LMA nor
1798 * "fid" xattr, then something crashed. We cannot re-generate the
1799 * FID directly, instead, the OI scrub will scan the OI structure
1800 * and try to re-generate the LMA from the OI mapping. But if the
1801 * OI mapping crashed or lost also, then we have to give up under
1802 * double failure cases. */
1803 scrub->os_convert_igif = 1;
1804 child = osd_ios_lookup_one_len(dot_lustre_name, dentry,
1805 strlen(dot_lustre_name));
1806 if (IS_ERR(child)) {
1807 rc = PTR_ERR(child);
1808 if (rc == -ENOENT) {
1809 /* It is 1.8 MDT device. */
1810 if (!(sf->sf_flags & SF_UPGRADE)) {
1811 osd_scrub_file_reset(scrub,
1812 LDISKFS_SB(osd_sb(dev))->s_es->s_uuid,
1814 sf->sf_internal_flags &= ~SIF_NO_HANDLE_OLD_FID;
1815 rc = osd_scrub_file_store(scrub);
1821 /* For lustre-2.x (x <= 3), the ".lustre" has NO FID-in-LMA,
1822 * so the client will get IGIF for the ".lustre" object when
1825 * From the OI scrub view, when the MDT upgrade to Lustre-2.4,
1826 * it does not know whether there are some old clients cached
1827 * the ".lustre" IGIF during the upgrading. Two choices:
1829 * 1) Generate IGIF-in-LMA and IGIF-in-OI for the ".lustre".
1830 * It will allow the old connected clients to access the
1831 * ".lustre" with cached IGIF. But it will cause others
1832 * on the MDT failed to check "fid_is_dot_lustre()".
1834 * 2) Use fixed FID {FID_SEQ_DOT_LUSTRE, FID_OID_DOT_LUSTRE, 0}
1835 * for ".lustre" in spite of whether there are some clients
1836 * cached the ".lustre" IGIF or not. It enables the check
1837 * "fid_is_dot_lustre()" on the MDT, although it will cause
1838 * that the old connected clients cannot access the ".lustre"
1839 * with the cached IGIF.
1841 * Usually, it is rare case for the old connected clients
1842 * to access the ".lustre" with cached IGIF. So we prefer
1843 * to the solution 2). */
1844 rc = osd_ios_scan_one(info, dev, child->d_inode,
1845 &LU_DOT_LUSTRE_FID, 0);
1847 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
1856 osd_ios_OBJECTS_scan(struct osd_thread_info *info, struct osd_device *dev,
1857 struct dentry *dentry, filldir_t filldir)
1859 struct osd_scrub *scrub = &dev->od_scrub;
1860 struct scrub_file *sf = &scrub->os_file;
1861 struct dentry *child;
1865 if (unlikely(sf->sf_internal_flags & SIF_NO_HANDLE_OLD_FID)) {
1866 sf->sf_internal_flags &= ~SIF_NO_HANDLE_OLD_FID;
1867 rc = osd_scrub_file_store(scrub);
1872 child = osd_ios_lookup_one_len(ADMIN_USR, dentry, strlen(ADMIN_USR));
1873 if (!IS_ERR(child)) {
1874 rc = osd_ios_scan_one(info, dev, child->d_inode, NULL, 0);
1877 rc = PTR_ERR(child);
1880 if (rc != 0 && rc != -ENOENT)
1883 child = osd_ios_lookup_one_len(ADMIN_GRP, dentry, strlen(ADMIN_GRP));
1884 if (!IS_ERR(child)) {
1885 rc = osd_ios_scan_one(info, dev, child->d_inode, NULL, 0);
1888 rc = PTR_ERR(child);
1897 static int osd_initial_OI_scrub(struct osd_thread_info *info,
1898 struct osd_device *dev)
1900 struct osd_ios_item *item = NULL;
1901 scandir_t scandir = osd_ios_general_scan;
1902 filldir_t filldir = osd_ios_root_fill;
1903 struct dentry *dentry = osd_sb(dev)->s_root;
1904 const struct osd_lf_map *map = osd_lf_maps;
1908 /* Lookup IGIF in OI by force for initial OI scrub. */
1909 dev->od_igif_inoi = 1;
1912 rc = scandir(info, dev, dentry, filldir);
1914 dput(item->oii_dentry);
1921 if (cfs_list_empty(&dev->od_ios_list))
1924 item = cfs_list_entry(dev->od_ios_list.next,
1925 struct osd_ios_item, oii_list);
1926 cfs_list_del_init(&item->oii_list);
1928 LASSERT(item->oii_scandir != NULL);
1929 scandir = item->oii_scandir;
1930 filldir = item->oii_filldir;
1931 dentry = item->oii_dentry;
1934 while (!cfs_list_empty(&dev->od_ios_list)) {
1935 item = cfs_list_entry(dev->od_ios_list.next,
1936 struct osd_ios_item, oii_list);
1937 cfs_list_del_init(&item->oii_list);
1938 dput(item->oii_dentry);
1945 /* There maybe the case that the object has been removed, but its OI
1946 * mapping is still in the OI file, such as the "CATALOGS" after MDT
1947 * file-level backup/restore. So here cleanup the stale OI mappings. */
1948 while (map->olm_name != NULL) {
1949 struct dentry *child;
1951 if (fid_is_zero(&map->olm_fid)) {
1956 child = osd_ios_lookup_one_len(map->olm_name,
1957 osd_sb(dev)->s_root,
1958 strlen(map->olm_name));
1961 else if (PTR_ERR(child) == -ENOENT)
1962 osd_scrub_refresh_mapping(info, dev, &map->olm_fid,
1963 NULL, DTO_INDEX_DELETE,
1971 char *osd_lf_fid2name(const struct lu_fid *fid)
1973 const struct osd_lf_map *map = osd_lf_maps;
1975 while (map->olm_name != NULL) {
1976 if (!lu_fid_eq(fid, &map->olm_fid)) {
1981 if (map->olm_flags & OLF_SHOW_NAME)
1982 return map->olm_name;
1990 /* OI scrub start/stop */
1992 static int do_osd_scrub_start(struct osd_device *dev, __u32 flags)
1994 struct osd_scrub *scrub = &dev->od_scrub;
1995 struct ptlrpc_thread *thread = &scrub->os_thread;
1996 struct l_wait_info lwi = { 0 };
2001 /* os_lock: sync status between stop and scrub thread */
2002 spin_lock(&scrub->os_lock);
2003 if (thread_is_running(thread)) {
2004 spin_unlock(&scrub->os_lock);
2006 } else if (unlikely(thread_is_stopping(thread))) {
2007 spin_unlock(&scrub->os_lock);
2008 l_wait_event(thread->t_ctl_waitq,
2009 thread_is_stopped(thread),
2013 spin_unlock(&scrub->os_lock);
2015 if (scrub->os_file.sf_status == SS_COMPLETED)
2018 scrub->os_start_flags = flags;
2019 thread_set_flags(thread, 0);
2020 rc = PTR_ERR(kthread_run(osd_scrub_main, dev, "OI_scrub"));
2021 if (IS_ERR_VALUE(rc)) {
2022 CERROR("%.16s: cannot start iteration thread, rc = %d\n",
2023 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, rc);
2027 l_wait_event(thread->t_ctl_waitq,
2028 thread_is_running(thread) || thread_is_stopped(thread),
2034 int osd_scrub_start(struct osd_device *dev)
2039 /* od_otable_mutex: prevent curcurrent start/stop */
2040 mutex_lock(&dev->od_otable_mutex);
2041 rc = do_osd_scrub_start(dev, SS_AUTO);
2042 mutex_unlock(&dev->od_otable_mutex);
2044 RETURN(rc == -EALREADY ? 0 : rc);
2047 static void do_osd_scrub_stop(struct osd_scrub *scrub)
2049 struct ptlrpc_thread *thread = &scrub->os_thread;
2050 struct l_wait_info lwi = { 0 };
2052 /* os_lock: sync status between stop and scrub thread */
2053 spin_lock(&scrub->os_lock);
2054 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
2055 thread_set_flags(thread, SVC_STOPPING);
2056 spin_unlock(&scrub->os_lock);
2057 wake_up_all(&thread->t_ctl_waitq);
2058 l_wait_event(thread->t_ctl_waitq,
2059 thread_is_stopped(thread),
2061 /* Do not skip the last lock/unlock, which can guarantee that
2062 * the caller cannot return until the OI scrub thread exit. */
2063 spin_lock(&scrub->os_lock);
2065 spin_unlock(&scrub->os_lock);
2068 static void osd_scrub_stop(struct osd_device *dev)
2070 /* od_otable_mutex: prevent curcurrent start/stop */
2071 mutex_lock(&dev->od_otable_mutex);
2072 dev->od_scrub.os_paused = 1;
2073 do_osd_scrub_stop(&dev->od_scrub);
2074 mutex_unlock(&dev->od_otable_mutex);
2077 /* OI scrub setup/cleanup */
2079 static const char osd_scrub_name[] = "OI_scrub";
2081 int osd_scrub_setup(const struct lu_env *env, struct osd_device *dev)
2083 struct osd_thread_info *info = osd_oti_get(env);
2084 struct osd_scrub *scrub = &dev->od_scrub;
2085 struct lvfs_run_ctxt *ctxt = &scrub->os_ctxt;
2086 struct scrub_file *sf = &scrub->os_file;
2087 struct super_block *sb = osd_sb(dev);
2088 struct ldiskfs_super_block *es = LDISKFS_SB(sb)->s_es;
2089 struct lvfs_run_ctxt saved;
2091 struct inode *inode;
2092 struct lu_fid *fid = &info->oti_fid;
2097 memset(scrub, 0, sizeof(*scrub));
2098 OBD_SET_CTXT_MAGIC(ctxt);
2099 ctxt->pwdmnt = dev->od_mnt;
2100 ctxt->pwd = dev->od_mnt->mnt_root;
2101 ctxt->fs = get_ds();
2103 init_waitqueue_head(&scrub->os_thread.t_ctl_waitq);
2104 init_rwsem(&scrub->os_rwsem);
2105 spin_lock_init(&scrub->os_lock);
2106 CFS_INIT_LIST_HEAD(&scrub->os_inconsistent_items);
2108 push_ctxt(&saved, ctxt);
2109 filp = filp_open(osd_scrub_name, O_RDWR | O_CREAT, 0644);
2111 pop_ctxt(&saved, ctxt);
2112 RETURN(PTR_ERR(filp));
2115 inode = filp->f_dentry->d_inode;
2116 /* 'What the @fid is' is not imporatant, because the object
2117 * has no OI mapping, and only is visible inside the OSD.*/
2118 lu_igif_build(fid, inode->i_ino, inode->i_generation);
2119 rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
2121 filp_close(filp, 0);
2122 pop_ctxt(&saved, ctxt);
2126 scrub->os_inode = igrab(inode);
2127 filp_close(filp, 0);
2128 pop_ctxt(&saved, ctxt);
2130 rc = osd_scrub_file_load(scrub);
2131 if (rc == -ENOENT) {
2132 osd_scrub_file_init(scrub, es->s_uuid);
2133 /* If the "/O" dir does not exist when mount (indicated by
2134 * osd_device::od_maybe_new), neither for the "/OI_scrub",
2135 * then it is quite probably that the device is a new one,
2136 * under such case, mark it as SIF_NO_HANDLE_OLD_FID.
2138 * For the rare case that "/O" and "OI_scrub" both lost on
2139 * an old device, it can be found and cleared later.
2141 * For the system with "SIF_NO_HANDLE_OLD_FID", we do not
2142 * need to check "filter_fid_old" and to convert it to
2143 * "filter_fid" for each object, and all the IGIF should
2144 * have their FID mapping in OI files already. */
2145 if (dev->od_maybe_new)
2146 sf->sf_internal_flags = SIF_NO_HANDLE_OLD_FID;
2148 } else if (rc != 0) {
2151 if (memcmp(sf->sf_uuid, es->s_uuid, 16) != 0) {
2152 osd_scrub_file_reset(scrub, es->s_uuid,SF_INCONSISTENT);
2154 } else if (sf->sf_status == SS_SCANNING) {
2155 sf->sf_status = SS_CRASHED;
2160 if (sf->sf_pos_last_checkpoint != 0)
2161 scrub->os_pos_current = sf->sf_pos_last_checkpoint + 1;
2163 scrub->os_pos_current = LDISKFS_FIRST_INO(sb) + 1;
2166 rc = osd_scrub_file_store(scrub);
2171 /* Initialize OI files. */
2172 rc = osd_oi_init(info, dev);
2176 rc = osd_initial_OI_scrub(info, dev);
2178 if (sf->sf_flags & SF_UPGRADE ||
2179 !(sf->sf_internal_flags & SIF_NO_HANDLE_OLD_FID ||
2180 sf->sf_success_count > 0)) {
2181 dev->od_igif_inoi = 0;
2182 dev->od_check_ff = dev->od_is_ost;
2184 dev->od_igif_inoi = 1;
2185 dev->od_check_ff = 0;
2188 if (sf->sf_flags & SF_INCONSISTENT)
2189 /* The 'od_igif_inoi' will be set under the
2191 * 1) new created system, or
2192 * 2) restored from file-level backup, or
2193 * 3) the upgrading completed.
2195 * The 'od_igif_inoi' may be cleared by OI scrub
2196 * later if found that the system is upgrading. */
2197 dev->od_igif_inoi = 1;
2199 if (!dev->od_noscrub &&
2200 ((sf->sf_status == SS_PAUSED) ||
2201 (sf->sf_status == SS_CRASHED &&
2202 sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
2203 SF_UPGRADE | SF_AUTO)) ||
2204 (sf->sf_status == SS_INIT &&
2205 sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
2207 rc = osd_scrub_start(dev);
2210 /* it is possible that dcache entries may keep objects after they are
2211 * deleted by OSD. While it looks safe this can cause object data to
2212 * stay until umount causing failures in tests calculating free space,
2213 * e.g. replay-ost-single. Since those dcache entries are not used
2214 * anymore let's just free them after use here */
2215 shrink_dcache_sb(sb);
2220 void osd_scrub_cleanup(const struct lu_env *env, struct osd_device *dev)
2222 struct osd_scrub *scrub = &dev->od_scrub;
2224 LASSERT(dev->od_otable_it == NULL);
2226 if (scrub->os_inode != NULL) {
2227 osd_scrub_stop(dev);
2228 iput(scrub->os_inode);
2229 scrub->os_inode = NULL;
2231 if (dev->od_oi_table != NULL)
2232 osd_oi_fini(osd_oti_get(env), dev);
2235 /* object table based iteration APIs */
2237 static struct dt_it *osd_otable_it_init(const struct lu_env *env,
2238 struct dt_object *dt, __u32 attr,
2239 struct lustre_capa *capa)
2241 enum dt_otable_it_flags flags = attr >> DT_OTABLE_IT_FLAGS_SHIFT;
2242 enum dt_otable_it_valid valid = attr & ~DT_OTABLE_IT_FLAGS_MASK;
2243 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
2244 struct osd_scrub *scrub = &dev->od_scrub;
2245 struct osd_otable_it *it;
2250 /* od_otable_mutex: prevent curcurrent init/fini */
2251 mutex_lock(&dev->od_otable_mutex);
2252 if (dev->od_otable_it != NULL)
2253 GOTO(out, it = ERR_PTR(-EALREADY));
2257 GOTO(out, it = ERR_PTR(-ENOMEM));
2259 dev->od_otable_it = it;
2261 it->ooi_cache.ooc_consumer_idx = -1;
2262 if (flags & DOIF_OUTUSED)
2263 it->ooi_used_outside = 1;
2265 if (flags & DOIF_RESET)
2268 if (valid & DOIV_ERROR_HANDLE) {
2269 if (flags & DOIF_FAILOUT)
2270 start |= SS_SET_FAILOUT;
2272 start |= SS_CLEAR_FAILOUT;
2275 if (valid & DOIV_DRYRUN) {
2276 if (flags & DOIF_DRYRUN)
2277 start |= SS_SET_DRYRUN;
2279 start |= SS_CLEAR_DRYRUN;
2282 rc = do_osd_scrub_start(dev, start);
2283 if (rc < 0 && rc != -EALREADY) {
2284 dev->od_otable_it = NULL;
2286 GOTO(out, it = ERR_PTR(rc));
2289 it->ooi_cache.ooc_pos_preload = scrub->os_pos_current;
2294 mutex_unlock(&dev->od_otable_mutex);
2295 return (struct dt_it *)it;
2298 static void osd_otable_it_fini(const struct lu_env *env, struct dt_it *di)
2300 struct osd_otable_it *it = (struct osd_otable_it *)di;
2301 struct osd_device *dev = it->ooi_dev;
2303 /* od_otable_mutex: prevent curcurrent init/fini */
2304 mutex_lock(&dev->od_otable_mutex);
2305 do_osd_scrub_stop(&dev->od_scrub);
2306 LASSERT(dev->od_otable_it == it);
2308 dev->od_otable_it = NULL;
2309 mutex_unlock(&dev->od_otable_mutex);
2313 static int osd_otable_it_get(const struct lu_env *env,
2314 struct dt_it *di, const struct dt_key *key)
2319 static void osd_otable_it_put(const struct lu_env *env, struct dt_it *di)
2324 osd_otable_it_wakeup(struct osd_scrub *scrub, struct osd_otable_it *it)
2326 spin_lock(&scrub->os_lock);
2327 if (it->ooi_cache.ooc_pos_preload < scrub->os_pos_current ||
2328 scrub->os_waiting ||
2329 !thread_is_running(&scrub->os_thread))
2330 it->ooi_waiting = 0;
2332 it->ooi_waiting = 1;
2333 spin_unlock(&scrub->os_lock);
2335 return !it->ooi_waiting;
2338 static int osd_otable_it_next(const struct lu_env *env, struct dt_it *di)
2340 struct osd_otable_it *it = (struct osd_otable_it *)di;
2341 struct osd_device *dev = it->ooi_dev;
2342 struct osd_scrub *scrub = &dev->od_scrub;
2343 struct osd_otable_cache *ooc = &it->ooi_cache;
2344 struct ptlrpc_thread *thread = &scrub->os_thread;
2345 struct l_wait_info lwi = { 0 };
2349 LASSERT(it->ooi_user_ready);
2352 if (!thread_is_running(thread) && !it->ooi_used_outside)
2355 if (ooc->ooc_cached_items > 0) {
2356 ooc->ooc_cached_items--;
2357 ooc->ooc_consumer_idx = (ooc->ooc_consumer_idx + 1) &
2358 ~OSD_OTABLE_IT_CACHE_MASK;
2362 if (it->ooi_all_cached) {
2363 l_wait_event(thread->t_ctl_waitq,
2364 !thread_is_running(thread),
2369 if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
2370 spin_lock(&scrub->os_lock);
2371 scrub->os_waiting = 0;
2372 wake_up_all(&scrub->os_thread.t_ctl_waitq);
2373 spin_unlock(&scrub->os_lock);
2376 if (it->ooi_cache.ooc_pos_preload >= scrub->os_pos_current)
2377 l_wait_event(thread->t_ctl_waitq,
2378 osd_otable_it_wakeup(scrub, it),
2381 if (!thread_is_running(thread) && !it->ooi_used_outside)
2384 rc = osd_otable_it_preload(env, it);
2391 static struct dt_key *osd_otable_it_key(const struct lu_env *env,
2392 const struct dt_it *di)
2397 static int osd_otable_it_key_size(const struct lu_env *env,
2398 const struct dt_it *di)
2400 return sizeof(__u64);
2403 static int osd_otable_it_rec(const struct lu_env *env, const struct dt_it *di,
2404 struct dt_rec *rec, __u32 attr)
2406 struct osd_otable_it *it = (struct osd_otable_it *)di;
2407 struct osd_otable_cache *ooc = &it->ooi_cache;
2409 *(struct lu_fid *)rec = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_fid;
2411 /* Filter out Invald FID already. */
2412 LASSERTF(fid_is_sane((struct lu_fid *)rec),
2413 "Invalid FID "DFID", p_idx = %d, c_idx = %d\n",
2414 PFID((struct lu_fid *)rec),
2415 ooc->ooc_producer_idx, ooc->ooc_consumer_idx);
2420 static __u64 osd_otable_it_store(const struct lu_env *env,
2421 const struct dt_it *di)
2423 struct osd_otable_it *it = (struct osd_otable_it *)di;
2424 struct osd_otable_cache *ooc = &it->ooi_cache;
2427 if (it->ooi_user_ready && ooc->ooc_consumer_idx != -1)
2428 hash = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_lid.oii_ino;
2430 hash = ooc->ooc_pos_preload;
2435 * Set the OSD layer iteration start position as the specified hash.
2437 static int osd_otable_it_load(const struct lu_env *env,
2438 const struct dt_it *di, __u64 hash)
2440 struct osd_otable_it *it = (struct osd_otable_it *)di;
2441 struct osd_device *dev = it->ooi_dev;
2442 struct osd_otable_cache *ooc = &it->ooi_cache;
2443 struct osd_scrub *scrub = &dev->od_scrub;
2447 /* Forbid to set iteration position after iteration started. */
2448 if (it->ooi_user_ready)
2451 if (hash > OSD_OTABLE_MAX_HASH)
2452 hash = OSD_OTABLE_MAX_HASH;
2454 ooc->ooc_pos_preload = hash;
2455 if (ooc->ooc_pos_preload <= LDISKFS_FIRST_INO(osd_sb(dev)))
2456 ooc->ooc_pos_preload = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
2458 it->ooi_user_ready = 1;
2459 if (!scrub->os_full_speed)
2460 wake_up_all(&scrub->os_thread.t_ctl_waitq);
2462 /* Unplug OSD layer iteration by the first next() call. */
2463 rc = osd_otable_it_next(env, (struct dt_it *)it);
2468 static int osd_otable_it_key_rec(const struct lu_env *env,
2469 const struct dt_it *di, void *key_rec)
2474 const struct dt_index_operations osd_otable_ops = {
2476 .init = osd_otable_it_init,
2477 .fini = osd_otable_it_fini,
2478 .get = osd_otable_it_get,
2479 .put = osd_otable_it_put,
2480 .next = osd_otable_it_next,
2481 .key = osd_otable_it_key,
2482 .key_size = osd_otable_it_key_size,
2483 .rec = osd_otable_it_rec,
2484 .store = osd_otable_it_store,
2485 .load = osd_otable_it_load,
2486 .key_rec = osd_otable_it_key_rec,
2490 /* high priority inconsistent items list APIs */
2492 int osd_oii_insert(struct osd_device *dev, struct osd_idmap_cache *oic,
2495 struct osd_inconsistent_item *oii;
2496 struct osd_scrub *scrub = &dev->od_scrub;
2497 struct ptlrpc_thread *thread = &scrub->os_thread;
2502 if (unlikely(oii == NULL))
2505 CFS_INIT_LIST_HEAD(&oii->oii_list);
2506 oii->oii_cache = *oic;
2507 oii->oii_insert = insert;
2509 spin_lock(&scrub->os_lock);
2510 if (unlikely(!thread_is_running(thread))) {
2511 spin_unlock(&scrub->os_lock);
2516 if (cfs_list_empty(&scrub->os_inconsistent_items))
2518 cfs_list_add_tail(&oii->oii_list, &scrub->os_inconsistent_items);
2519 spin_unlock(&scrub->os_lock);
2522 wake_up_all(&thread->t_ctl_waitq);
2527 int osd_oii_lookup(struct osd_device *dev, const struct lu_fid *fid,
2528 struct osd_inode_id *id)
2530 struct osd_scrub *scrub = &dev->od_scrub;
2531 struct osd_inconsistent_item *oii;
2534 spin_lock(&scrub->os_lock);
2535 cfs_list_for_each_entry(oii, &scrub->os_inconsistent_items, oii_list) {
2536 if (lu_fid_eq(fid, &oii->oii_cache.oic_fid)) {
2537 *id = oii->oii_cache.oic_lid;
2538 spin_unlock(&scrub->os_lock);
2542 spin_unlock(&scrub->os_lock);
2549 static const char *scrub_status_names[] = {
2560 static const char *scrub_flags_names[] = {
2568 static const char *scrub_param_names[] = {
2574 static int scrub_bits_dump(char **buf, int *len, int bits, const char *names[],
2582 rc = snprintf(*buf, *len, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
2588 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
2591 rc = snprintf(*buf, *len, "%s%c", names[i],
2592 bits != 0 ? ',' : '\n');
2603 static int scrub_time_dump(char **buf, int *len, __u64 time, const char *prefix)
2608 rc = snprintf(*buf, *len, "%s: "LPU64" seconds\n", prefix,
2609 cfs_time_current_sec() - time);
2611 rc = snprintf(*buf, *len, "%s: N/A\n", prefix);
2620 static int scrub_pos_dump(char **buf, int *len, __u64 pos, const char *prefix)
2625 rc = snprintf(*buf, *len, "%s: "LPU64"\n", prefix, pos);
2627 rc = snprintf(*buf, *len, "%s: N/A\n", prefix);
2636 int osd_scrub_dump(struct osd_device *dev, char *buf, int len)
2638 struct osd_scrub *scrub = &dev->od_scrub;
2639 struct scrub_file *sf = &scrub->os_file;
2646 down_read(&scrub->os_rwsem);
2647 rc = snprintf(buf, len,
2652 sf->sf_magic, (int)sf->sf_oi_count,
2653 scrub_status_names[sf->sf_status]);
2659 rc = scrub_bits_dump(&buf, &len, sf->sf_flags, scrub_flags_names,
2664 rc = scrub_bits_dump(&buf, &len, sf->sf_param, scrub_param_names,
2669 rc = scrub_time_dump(&buf, &len, sf->sf_time_last_complete,
2670 "time_since_last_completed");
2674 rc = scrub_time_dump(&buf, &len, sf->sf_time_latest_start,
2675 "time_since_latest_start");
2679 rc = scrub_time_dump(&buf, &len, sf->sf_time_last_checkpoint,
2680 "time_since_last_checkpoint");
2684 rc = scrub_pos_dump(&buf, &len, sf->sf_pos_latest_start,
2685 "latest_start_position");
2689 rc = scrub_pos_dump(&buf, &len, sf->sf_pos_last_checkpoint,
2690 "last_checkpoint_position");
2694 rc = scrub_pos_dump(&buf, &len, sf->sf_pos_first_inconsistent,
2695 "first_failure_position");
2699 checked = sf->sf_items_checked + scrub->os_new_checked;
2700 rc = snprintf(buf, len,
2701 "checked: "LPU64"\n"
2702 "updated: "LPU64"\n"
2704 "prior_updated: "LPU64"\n"
2705 "noscrub: "LPU64"\n"
2707 "success_count: %u\n",
2708 checked, sf->sf_items_updated, sf->sf_items_failed,
2709 sf->sf_items_updated_prior, sf->sf_items_noscrub,
2710 sf->sf_items_igif, sf->sf_success_count);
2717 if (thread_is_running(&scrub->os_thread)) {
2718 cfs_duration_t duration = cfs_time_current() -
2719 scrub->os_time_last_checkpoint;
2720 __u64 new_checked = scrub->os_new_checked * HZ;
2721 __u32 rtime = sf->sf_run_time +
2722 cfs_duration_sec(duration + HALF_SEC);
2725 do_div(new_checked, duration);
2727 do_div(speed, rtime);
2728 rc = snprintf(buf, len,
2729 "run_time: %u seconds\n"
2730 "average_speed: "LPU64" objects/sec\n"
2731 "real-time_speed: "LPU64" objects/sec\n"
2732 "current_position: %u\n"
2733 "lf_scanned: "LPU64"\n"
2734 "lf_reparied: "LPU64"\n"
2735 "lf_failed: "LPU64"\n",
2736 rtime, speed, new_checked, scrub->os_pos_current,
2737 scrub->os_lf_scanned, scrub->os_lf_repaired,
2738 scrub->os_lf_failed);
2740 if (sf->sf_run_time != 0)
2741 do_div(speed, sf->sf_run_time);
2742 rc = snprintf(buf, len,
2743 "run_time: %u seconds\n"
2744 "average_speed: "LPU64" objects/sec\n"
2745 "real-time_speed: N/A\n"
2746 "current_position: N/A\n"
2747 "lf_scanned: "LPU64"\n"
2748 "lf_reparied: "LPU64"\n"
2749 "lf_failed: "LPU64"\n",
2750 sf->sf_run_time, speed, scrub->os_lf_scanned,
2751 scrub->os_lf_repaired, scrub->os_lf_failed);
2761 up_read(&scrub->os_rwsem);