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, 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>
39 # define EXPORT_SYMTAB
41 #define DEBUG_SUBSYSTEM S_MDS
43 #include <lustre/lustre_idl.h>
44 #include <lustre_disk.h>
45 #include <dt_object.h>
47 #include "osd_internal.h"
49 #include "osd_scrub.h"
51 #define HALF_SEC (CFS_HZ >> 1)
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 */
64 static inline struct osd_device *osd_scrub2dev(struct osd_scrub *scrub)
66 return container_of0(scrub, struct osd_device, od_scrub);
69 static inline struct super_block *osd_scrub2sb(struct osd_scrub *scrub)
71 return osd_sb(osd_scrub2dev(scrub));
74 static inline int osd_scrub_has_window(struct osd_scrub *scrub,
75 struct osd_otable_cache *ooc)
77 return scrub->os_pos_current < ooc->ooc_pos_preload + SCRUB_WINDOW_SIZE;
80 static int osd_scrub_refresh_mapping(struct osd_thread_info *info,
81 struct osd_device *dev,
82 const struct lu_fid *fid,
83 const struct osd_inode_id *id, int ops)
85 struct lu_fid *oi_fid = &info->oti_fid2;
86 struct osd_inode_id *oi_id = &info->oti_id2;
87 struct iam_container *bag;
88 struct iam_path_descr *ipd;
93 fid_cpu_to_be(oi_fid, fid);
94 osd_id_pack(oi_id, id);
95 jh = ldiskfs_journal_start_sb(osd_sb(dev),
96 osd_dto_credits_noquota[ops]);
99 CERROR("%.16s: fail to start trans for scrub store: rc = %d\n",
100 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, rc);
104 bag = &osd_fid2oi(dev, fid)->oi_dir.od_container;
105 ipd = osd_idx_ipd_get(info->oti_env, bag);
106 if (unlikely(ipd == NULL)) {
107 ldiskfs_journal_stop(jh);
108 CERROR("%.16s: fail to get ipd for scrub store\n",
109 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name);
113 if (ops == DTO_INDEX_UPDATE) {
114 rc = iam_update(jh, bag, (const struct iam_key *)oi_fid,
115 (struct iam_rec *)oi_id, ipd);
117 rc = iam_insert(jh, bag, (const struct iam_key *)oi_fid,
118 (struct iam_rec *)oi_id, ipd);
121 /* XXX: There are trouble things when adding OI
122 * mapping for IGIF object, which may cause
123 * multiple objects to be mapped to the same
124 * IGIF formatted FID. Consider the following
127 * 1) The MDT is upgrading from 1.8 device.
128 * The OI scrub generates IGIF FID1 for the
129 * OBJ1 and adds the OI mapping.
131 * 2) For some reason, the OI scrub does not
132 * process all the IGIF objects completely.
134 * 3) The MDT is backuped and restored against
137 * 4) When the MDT mounts up, the OI scrub will
138 * try to rebuild the OI files. For some IGIF
139 * object, OBJ2, which was not processed by the
140 * OI scrub before the backup/restore, and the
141 * new generated IGIF formatted FID may be just
142 * the FID1, the same as OBJ1.
144 * Under such case, the OI scrub cannot know how
145 * to generate new FID for the OBJ2.
147 * Currently, we do nothing for that. One possible
148 * solution is to generate new normal FID for the
151 * Anyway, it is rare, only exists in theory. */
154 osd_ipd_put(info->oti_env, bag, ipd);
155 ldiskfs_journal_stop(jh);
159 /* OI_scrub file ops */
161 static void osd_scrub_file_to_cpu(struct scrub_file *des,
162 struct scrub_file *src)
164 memcpy(des->sf_uuid, src->sf_uuid, 16);
165 des->sf_flags = le64_to_cpu(src->sf_flags);
166 des->sf_magic = le32_to_cpu(src->sf_magic);
167 des->sf_status = le16_to_cpu(src->sf_status);
168 des->sf_param = le16_to_cpu(src->sf_param);
169 des->sf_time_last_complete =
170 le64_to_cpu(src->sf_time_last_complete);
171 des->sf_time_latest_start =
172 le64_to_cpu(src->sf_time_latest_start);
173 des->sf_time_last_checkpoint =
174 le64_to_cpu(src->sf_time_last_checkpoint);
175 des->sf_pos_latest_start =
176 le64_to_cpu(src->sf_pos_latest_start);
177 des->sf_pos_last_checkpoint =
178 le64_to_cpu(src->sf_pos_last_checkpoint);
179 des->sf_pos_first_inconsistent =
180 le64_to_cpu(src->sf_pos_first_inconsistent);
181 des->sf_items_checked =
182 le64_to_cpu(src->sf_items_checked);
183 des->sf_items_updated =
184 le64_to_cpu(src->sf_items_updated);
185 des->sf_items_failed =
186 le64_to_cpu(src->sf_items_failed);
187 des->sf_items_updated_prior =
188 le64_to_cpu(src->sf_items_updated_prior);
189 des->sf_run_time = le32_to_cpu(src->sf_run_time);
190 des->sf_success_count = le32_to_cpu(src->sf_success_count);
191 des->sf_oi_count = le16_to_cpu(src->sf_oi_count);
192 memcpy(des->sf_oi_bitmap, src->sf_oi_bitmap, SCRUB_OI_BITMAP_SIZE);
195 static void osd_scrub_file_to_le(struct scrub_file *des,
196 struct scrub_file *src)
198 memcpy(des->sf_uuid, src->sf_uuid, 16);
199 des->sf_flags = cpu_to_le64(src->sf_flags);
200 des->sf_magic = cpu_to_le32(src->sf_magic);
201 des->sf_status = cpu_to_le16(src->sf_status);
202 des->sf_param = cpu_to_le16(src->sf_param);
203 des->sf_time_last_complete =
204 cpu_to_le64(src->sf_time_last_complete);
205 des->sf_time_latest_start =
206 cpu_to_le64(src->sf_time_latest_start);
207 des->sf_time_last_checkpoint =
208 cpu_to_le64(src->sf_time_last_checkpoint);
209 des->sf_pos_latest_start =
210 cpu_to_le64(src->sf_pos_latest_start);
211 des->sf_pos_last_checkpoint =
212 cpu_to_le64(src->sf_pos_last_checkpoint);
213 des->sf_pos_first_inconsistent =
214 cpu_to_le64(src->sf_pos_first_inconsistent);
215 des->sf_items_checked =
216 cpu_to_le64(src->sf_items_checked);
217 des->sf_items_updated =
218 cpu_to_le64(src->sf_items_updated);
219 des->sf_items_failed =
220 cpu_to_le64(src->sf_items_failed);
221 des->sf_items_updated_prior =
222 cpu_to_le64(src->sf_items_updated_prior);
223 des->sf_run_time = cpu_to_le32(src->sf_run_time);
224 des->sf_success_count = cpu_to_le32(src->sf_success_count);
225 des->sf_oi_count = cpu_to_le16(src->sf_oi_count);
226 memcpy(des->sf_oi_bitmap, src->sf_oi_bitmap, SCRUB_OI_BITMAP_SIZE);
229 static void osd_scrub_file_init(struct osd_scrub *scrub, __u8 *uuid)
231 struct scrub_file *sf = &scrub->os_file;
233 memset(sf, 0, sizeof(*sf));
234 memcpy(sf->sf_uuid, uuid, 16);
235 sf->sf_magic = SCRUB_MAGIC_V1;
236 sf->sf_status = SS_INIT;
239 void osd_scrub_file_reset(struct osd_scrub *scrub, __u8 *uuid, __u64 flags)
241 struct scrub_file *sf = &scrub->os_file;
243 CDEBUG(D_LFSCK, "Reset OI scrub file, flags = "LPX64"\n", flags);
244 memcpy(sf->sf_uuid, uuid, 16);
245 sf->sf_status = SS_INIT;
246 sf->sf_flags |= flags;
249 sf->sf_time_latest_start = 0;
250 sf->sf_time_last_checkpoint = 0;
251 sf->sf_pos_latest_start = 0;
252 sf->sf_pos_last_checkpoint = 0;
253 sf->sf_pos_first_inconsistent = 0;
254 sf->sf_items_checked = 0;
255 sf->sf_items_updated = 0;
256 sf->sf_items_failed = 0;
257 sf->sf_items_updated_prior = 0;
258 sf->sf_items_noscrub = 0;
259 sf->sf_items_igif = 0;
262 static int osd_scrub_file_load(struct osd_scrub *scrub)
265 char *name = LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name;
266 int len = sizeof(scrub->os_file_disk);
269 rc = osd_ldiskfs_read(scrub->os_inode, &scrub->os_file_disk, len, &pos);
271 struct scrub_file *sf = &scrub->os_file;
273 osd_scrub_file_to_cpu(sf, &scrub->os_file_disk);
274 if (sf->sf_magic != SCRUB_MAGIC_V1) {
275 CWARN("%.16s: invalid scrub magic 0x%x != 0x%x\n,",
276 name, sf->sf_magic, SCRUB_MAGIC_V1);
277 /* Process it as new scrub file. */
282 } else if (rc != 0) {
283 CERROR("%.16s: fail to load scrub file, expected = %d, "
284 "rc = %d\n", name, len, rc);
288 /* return -ENOENT for empty scrub file case. */
295 int osd_scrub_file_store(struct osd_scrub *scrub)
297 struct osd_device *dev;
300 int len = sizeof(scrub->os_file_disk);
304 dev = container_of0(scrub, struct osd_device, od_scrub);
305 credits = osd_dto_credits_noquota[DTO_WRITE_BASE] +
306 osd_dto_credits_noquota[DTO_WRITE_BLOCK];
307 jh = ldiskfs_journal_start_sb(osd_sb(dev), credits);
310 CERROR("%.16s: fail to start trans for scrub store, rc = %d\n",
311 LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,rc);
315 osd_scrub_file_to_le(&scrub->os_file_disk, &scrub->os_file);
316 rc = osd_ldiskfs_write_record(scrub->os_inode, &scrub->os_file_disk,
318 ldiskfs_journal_stop(jh);
320 CERROR("%.16s: fail to store scrub file, expected = %d, "
322 LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,
324 scrub->os_time_last_checkpoint = cfs_time_current();
325 scrub->os_time_next_checkpoint = scrub->os_time_last_checkpoint +
326 cfs_time_seconds(SCRUB_CHECKPOINT_INTERVAL);
332 static int osd_scrub_prep(struct osd_device *dev)
334 struct osd_scrub *scrub = &dev->od_scrub;
335 struct ptlrpc_thread *thread = &scrub->os_thread;
336 struct scrub_file *sf = &scrub->os_file;
337 __u32 flags = scrub->os_start_flags;
341 down_write(&scrub->os_rwsem);
342 if (flags & SS_SET_FAILOUT)
343 sf->sf_param |= SP_FAILOUT;
345 if (flags & SS_CLEAR_FAILOUT)
346 sf->sf_param &= ~SP_FAILOUT;
348 if (flags & SS_RESET)
349 osd_scrub_file_reset(scrub,
350 LDISKFS_SB(osd_sb(dev))->s_es->s_uuid, 0);
352 if (flags & SS_AUTO) {
353 scrub->os_full_speed = 1;
354 sf->sf_flags |= SF_AUTO;
356 scrub->os_full_speed = 0;
359 if (sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT | SF_UPGRADE))
360 scrub->os_full_speed = 1;
362 scrub->os_in_prior = 0;
363 scrub->os_waiting = 0;
364 scrub->os_paused = 0;
365 scrub->os_new_checked = 0;
366 if (sf->sf_pos_last_checkpoint != 0)
367 sf->sf_pos_latest_start = sf->sf_pos_last_checkpoint + 1;
369 sf->sf_pos_latest_start = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
371 scrub->os_pos_current = sf->sf_pos_latest_start;
372 sf->sf_status = SS_SCANNING;
373 sf->sf_time_latest_start = cfs_time_current_sec();
374 sf->sf_time_last_checkpoint = sf->sf_time_latest_start;
375 rc = osd_scrub_file_store(scrub);
377 spin_lock(&scrub->os_lock);
378 thread_set_flags(thread, SVC_RUNNING);
379 spin_unlock(&scrub->os_lock);
380 cfs_waitq_broadcast(&thread->t_ctl_waitq);
382 up_write(&scrub->os_rwsem);
388 osd_scrub_check_update(struct osd_thread_info *info, struct osd_device *dev,
389 struct osd_idmap_cache *oic, int val)
391 struct osd_scrub *scrub = &dev->od_scrub;
392 struct scrub_file *sf = &scrub->os_file;
393 struct lu_fid *fid = &oic->oic_fid;
394 struct osd_inode_id *lid = &oic->oic_lid;
395 struct osd_inode_id *lid2 = &info->oti_id;
396 struct osd_inconsistent_item *oii = NULL;
397 struct inode *inode = NULL;
398 int ops = DTO_INDEX_UPDATE;
403 down_write(&scrub->os_rwsem);
404 scrub->os_new_checked++;
408 if (scrub->os_in_prior)
409 oii = cfs_list_entry(oic, struct osd_inconsistent_item,
412 if (lid->oii_ino < sf->sf_pos_latest_start && oii == NULL)
415 if (fid_is_igif(fid))
418 if (val == SCRUB_NEXT_NOLMA && !dev->od_handle_nolma)
421 if ((oii != NULL && oii->oii_insert) || (val == SCRUB_NEXT_NOLMA))
424 /* XXX: Currently, no FID-in-LMA for OST object, so osd_oi_lookup()
425 * without checking FLD is enough.
427 * It should be updated if FID-in-LMA for OSD object introduced
429 rc = osd_oi_lookup(info, dev, fid, lid2, false);
435 inode = osd_iget(info, dev, lid);
438 /* Someone removed the inode. */
439 if (rc == -ENOENT || rc == -ESTALE)
444 /* Prevent the inode to be unlinked during OI scrub. */
445 mutex_lock(&inode->i_mutex);
446 if (unlikely(inode->i_nlink == 0)) {
447 mutex_unlock(&inode->i_mutex);
452 ops = DTO_INDEX_INSERT;
453 idx = osd_oi_fid2idx(dev, fid);
454 if (val == SCRUB_NEXT_NOLMA) {
455 rc = osd_ea_fid_set(info, inode, fid);
459 sf->sf_flags |= SF_RECREATED | SF_INCONSISTENT;
460 if (unlikely(!ldiskfs_test_bit(idx, sf->sf_oi_bitmap)))
461 ldiskfs_set_bit(idx, sf->sf_oi_bitmap);
463 } else if (osd_id_eq(lid, lid2)) {
466 sf->sf_flags |= SF_INCONSISTENT;
469 rc = osd_scrub_refresh_mapping(info, dev, fid, lid, ops);
471 if (scrub->os_in_prior)
472 sf->sf_items_updated_prior++;
474 sf->sf_items_updated++;
481 sf->sf_items_failed++;
482 if (sf->sf_pos_first_inconsistent == 0 ||
483 sf->sf_pos_first_inconsistent > lid->oii_ino)
484 sf->sf_pos_first_inconsistent = lid->oii_ino;
489 if (ops == DTO_INDEX_INSERT) {
490 mutex_unlock(&inode->i_mutex);
493 up_write(&scrub->os_rwsem);
496 LASSERT(!cfs_list_empty(&oii->oii_list));
498 spin_lock(&scrub->os_lock);
499 cfs_list_del_init(&oii->oii_list);
500 spin_unlock(&scrub->os_lock);
503 RETURN(sf->sf_param & SP_FAILOUT ? rc : 0);
506 static int osd_scrub_checkpoint(struct osd_scrub *scrub)
508 struct scrub_file *sf = &scrub->os_file;
511 if (likely(cfs_time_before(cfs_time_current(),
512 scrub->os_time_next_checkpoint) ||
513 scrub->os_new_checked == 0))
516 down_write(&scrub->os_rwsem);
517 sf->sf_items_checked += scrub->os_new_checked;
518 scrub->os_new_checked = 0;
519 sf->sf_pos_last_checkpoint = scrub->os_pos_current;
520 sf->sf_time_last_checkpoint = cfs_time_current_sec();
521 sf->sf_run_time += cfs_duration_sec(cfs_time_current() + HALF_SEC -
522 scrub->os_time_last_checkpoint);
523 rc = osd_scrub_file_store(scrub);
524 up_write(&scrub->os_rwsem);
529 static void osd_scrub_post(struct osd_scrub *scrub, int result)
531 struct scrub_file *sf = &scrub->os_file;
534 down_write(&scrub->os_rwsem);
535 spin_lock(&scrub->os_lock);
536 thread_set_flags(&scrub->os_thread, SVC_STOPPING);
537 spin_unlock(&scrub->os_lock);
538 if (scrub->os_new_checked > 0) {
539 sf->sf_items_checked += scrub->os_new_checked;
540 scrub->os_new_checked = 0;
541 sf->sf_pos_last_checkpoint = scrub->os_pos_current;
543 sf->sf_time_last_checkpoint = cfs_time_current_sec();
545 sf->sf_status = SS_COMPLETED;
546 memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
547 sf->sf_flags &= ~(SF_RECREATED | SF_INCONSISTENT |
548 SF_UPGRADE | SF_AUTO);
549 sf->sf_time_last_complete = sf->sf_time_last_checkpoint;
550 sf->sf_success_count++;
551 } else if (result == 0) {
552 if (scrub->os_paused)
553 sf->sf_status = SS_PAUSED;
555 sf->sf_status = SS_STOPPED;
557 sf->sf_status = SS_FAILED;
559 sf->sf_run_time += cfs_duration_sec(cfs_time_current() + HALF_SEC -
560 scrub->os_time_last_checkpoint);
561 result = osd_scrub_file_store(scrub);
563 CERROR("%.16s: fail to osd_scrub_post, rc = %d\n",
564 LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,
566 up_write(&scrub->os_rwsem);
571 /* iteration engine */
573 struct osd_iit_param {
574 struct super_block *sb;
575 struct buffer_head *bitmap;
581 typedef int (*osd_iit_next_policy)(struct osd_thread_info *info,
582 struct osd_device *dev,
583 struct osd_iit_param *param,
584 struct osd_idmap_cache **oic,
587 typedef int (*osd_iit_exec_policy)(struct osd_thread_info *info,
588 struct osd_device *dev,
589 struct osd_iit_param *param,
590 struct osd_idmap_cache *oic,
591 int *noslot, int rc);
593 static int osd_iit_next(struct osd_iit_param *param, __u32 *pos)
595 param->offset = ldiskfs_find_next_bit(param->bitmap->b_data,
596 LDISKFS_INODES_PER_GROUP(param->sb), param->offset);
597 if (param->offset >= LDISKFS_INODES_PER_GROUP(param->sb)) {
598 *pos = 1 + (param->bg+1) * LDISKFS_INODES_PER_GROUP(param->sb);
599 return SCRUB_NEXT_BREAK;
601 *pos = param->gbase + param->offset;
606 static int osd_iit_iget(struct osd_thread_info *info, struct osd_device *dev,
607 struct lu_fid *fid, struct osd_inode_id *lid, __u32 pos,
608 struct super_block *sb, bool scrub)
610 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
614 osd_id_gen(lid, pos, OSD_OII_NOGEN);
615 inode = osd_iget(info, dev, lid);
618 /* The inode may be removed after bitmap searching, or the
619 * file is new created without inode initialized yet. */
620 if (rc == -ENOENT || rc == -ESTALE)
621 return SCRUB_NEXT_CONTINUE;
623 CERROR("%.16s: fail to read inode, ino# = %u, rc = %d\n",
624 LDISKFS_SB(sb)->s_es->s_volume_name, pos, rc);
628 /* If the inode has no OI mapping, then it is special locally used,
629 * should be invisible to OI scrub or up layer LFSCK. */
630 if (ldiskfs_test_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI)) {
632 return SCRUB_NEXT_CONTINUE;
636 ldiskfs_test_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB)) {
637 /* Only skip it for the first OI scrub accessing. */
638 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
640 return SCRUB_NEXT_NOSCRUB;
643 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
646 if (!fid_is_client_visible(&lma->lma_self_fid))
647 rc = SCRUB_NEXT_CONTINUE;
649 *fid = lma->lma_self_fid;
651 } else if (rc == -ENODATA) {
652 lu_igif_build(fid, inode->i_ino, inode->i_generation);
654 rc = SCRUB_NEXT_NOLMA;
662 static int osd_scrub_next(struct osd_thread_info *info, struct osd_device *dev,
663 struct osd_iit_param *param,
664 struct osd_idmap_cache **oic, int noslot)
666 struct osd_scrub *scrub = &dev->od_scrub;
667 struct ptlrpc_thread *thread = &scrub->os_thread;
669 struct osd_inode_id *lid;
672 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_DELAY) && cfs_fail_val > 0) {
673 struct l_wait_info lwi;
675 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val), NULL, NULL);
676 l_wait_event(thread->t_ctl_waitq,
677 !cfs_list_empty(&scrub->os_inconsistent_items) ||
678 !thread_is_running(thread),
682 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_CRASH)) {
683 spin_lock(&scrub->os_lock);
684 thread_set_flags(thread, SVC_STOPPING);
685 spin_unlock(&scrub->os_lock);
686 return SCRUB_NEXT_CRASH;
689 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_FATAL))
690 return SCRUB_NEXT_FATAL;
692 if (unlikely(!thread_is_running(thread)))
693 return SCRUB_NEXT_EXIT;
695 if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
696 struct osd_inconsistent_item *oii;
698 oii = cfs_list_entry(scrub->os_inconsistent_items.next,
699 struct osd_inconsistent_item, oii_list);
700 *oic = &oii->oii_cache;
701 scrub->os_in_prior = 1;
706 return SCRUB_NEXT_WAIT;
708 rc = osd_iit_next(param, &scrub->os_pos_current);
712 *oic = &scrub->os_oic;
713 fid = &(*oic)->oic_fid;
714 lid = &(*oic)->oic_lid;
715 rc = osd_iit_iget(info, dev, fid, lid,
716 scrub->os_pos_current, param->sb, true);
720 static int osd_preload_next(struct osd_thread_info *info,
721 struct osd_device *dev, struct osd_iit_param *param,
722 struct osd_idmap_cache **oic, int noslot)
724 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
725 struct osd_scrub *scrub;
726 struct ptlrpc_thread *thread;
729 rc = osd_iit_next(param, &ooc->ooc_pos_preload);
733 scrub = &dev->od_scrub;
734 thread = &scrub->os_thread;
735 if (thread_is_running(thread) &&
736 ooc->ooc_pos_preload >= scrub->os_pos_current)
737 return SCRUB_NEXT_EXIT;
739 rc = osd_iit_iget(info, dev,
740 &ooc->ooc_cache[ooc->ooc_producer_idx].oic_fid,
741 &ooc->ooc_cache[ooc->ooc_producer_idx].oic_lid,
742 ooc->ooc_pos_preload, param->sb, false);
743 /* If succeed, it needs to move forward; otherwise up layer LFSCK may
744 * ignore the failure, so it still need to skip the inode next time. */
745 ooc->ooc_pos_preload = param->gbase + ++(param->offset);
749 static int osd_scrub_exec(struct osd_thread_info *info, struct osd_device *dev,
750 struct osd_iit_param *param,
751 struct osd_idmap_cache *oic, int *noslot, int rc)
753 struct l_wait_info lwi = { 0 };
754 struct osd_scrub *scrub = &dev->od_scrub;
755 struct scrub_file *sf = &scrub->os_file;
756 struct ptlrpc_thread *thread = &scrub->os_thread;
757 struct osd_otable_it *it = dev->od_otable_it;
758 struct osd_otable_cache *ooc = it ? &it->ooi_cache : NULL;
761 case SCRUB_NEXT_CONTINUE:
763 case SCRUB_NEXT_WAIT:
765 case SCRUB_NEXT_NOSCRUB:
766 down_write(&scrub->os_rwsem);
767 scrub->os_new_checked++;
768 sf->sf_items_noscrub++;
769 up_write(&scrub->os_rwsem);
773 rc = osd_scrub_check_update(info, dev, oic, rc);
777 rc = osd_scrub_checkpoint(scrub);
779 CERROR("%.16s: fail to checkpoint, pos = %u, rc = %d\n",
780 LDISKFS_SB(param->sb)->s_es->s_volume_name,
781 scrub->os_pos_current, rc);
782 /* Continue, as long as the scrub itself can go ahead. */
785 if (scrub->os_in_prior) {
786 scrub->os_in_prior = 0;
791 scrub->os_pos_current = param->gbase + ++(param->offset);
792 if (it != NULL && it->ooi_waiting &&
793 ooc->ooc_pos_preload < scrub->os_pos_current) {
795 cfs_waitq_broadcast(&thread->t_ctl_waitq);
798 if (scrub->os_full_speed || rc == SCRUB_NEXT_CONTINUE)
802 if (osd_scrub_has_window(scrub, ooc)) {
807 scrub->os_waiting = 1;
808 l_wait_event(thread->t_ctl_waitq,
809 osd_scrub_has_window(scrub, ooc) ||
810 !cfs_list_empty(&scrub->os_inconsistent_items) ||
811 !thread_is_running(thread),
813 scrub->os_waiting = 0;
815 if (osd_scrub_has_window(scrub, ooc))
822 static int osd_preload_exec(struct osd_thread_info *info,
823 struct osd_device *dev, struct osd_iit_param *param,
824 struct osd_idmap_cache *oic, int *noslot, int rc)
826 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
829 ooc->ooc_cached_items++;
830 ooc->ooc_producer_idx = (ooc->ooc_producer_idx + 1) &
831 ~OSD_OTABLE_IT_CACHE_MASK;
833 return rc > 0 ? 0 : rc;
836 #define SCRUB_IT_ALL 1
837 #define SCRUB_IT_CRASH 2
839 static int osd_inode_iteration(struct osd_thread_info *info,
840 struct osd_device *dev, __u32 max, int preload)
842 osd_iit_next_policy next;
843 osd_iit_exec_policy exec;
846 struct osd_iit_param param;
853 struct osd_scrub *scrub = &dev->od_scrub;
855 next = osd_scrub_next;
856 exec = osd_scrub_exec;
857 pos = &scrub->os_pos_current;
858 count = &scrub->os_new_checked;
860 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
862 next = osd_preload_next;
863 exec = osd_preload_exec;
864 pos = &ooc->ooc_pos_preload;
865 count = &ooc->ooc_cached_items;
867 param.sb = osd_sb(dev);
868 limit = le32_to_cpu(LDISKFS_SB(param.sb)->s_es->s_inodes_count);
870 while (*pos <= limit && *count < max) {
871 struct osd_idmap_cache *oic = NULL;
873 param.bg = (*pos - 1) / LDISKFS_INODES_PER_GROUP(param.sb);
874 param.offset = (*pos - 1) % LDISKFS_INODES_PER_GROUP(param.sb);
875 param.gbase = 1 + param.bg * LDISKFS_INODES_PER_GROUP(param.sb);
876 param.bitmap = ldiskfs_read_inode_bitmap(param.sb, param.bg);
877 if (param.bitmap == NULL) {
878 CERROR("%.16s: fail to read bitmap for %u, "
879 "scrub will stop, urgent mode\n",
880 LDISKFS_SB(param.sb)->s_es->s_volume_name,
885 while (param.offset < LDISKFS_INODES_PER_GROUP(param.sb) &&
887 rc = next(info, dev, ¶m, &oic, noslot);
889 case SCRUB_NEXT_BREAK:
891 case SCRUB_NEXT_EXIT:
892 brelse(param.bitmap);
894 case SCRUB_NEXT_CRASH:
895 brelse(param.bitmap);
896 RETURN(SCRUB_IT_CRASH);
897 case SCRUB_NEXT_FATAL:
898 brelse(param.bitmap);
902 rc = exec(info, dev, ¶m, oic, &noslot, rc);
904 brelse(param.bitmap);
910 brelse(param.bitmap);
914 RETURN(SCRUB_IT_ALL);
918 static int osd_otable_it_preload(const struct lu_env *env,
919 struct osd_otable_it *it)
921 struct osd_device *dev = it->ooi_dev;
922 struct osd_scrub *scrub = &dev->od_scrub;
923 struct osd_otable_cache *ooc = &it->ooi_cache;
927 rc = osd_inode_iteration(osd_oti_get(env), dev,
928 OSD_OTABLE_IT_CACHE_SIZE, 1);
929 if (rc == SCRUB_IT_ALL)
930 it->ooi_all_cached = 1;
932 CDEBUG(D_LFSCK, "OSD pre-loaded: max = %u, preload = %u, rc = %d\n",
933 le32_to_cpu(LDISKFS_SB(osd_sb(dev))->s_es->s_inodes_count),
934 ooc->ooc_pos_preload, rc);
936 if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
937 scrub->os_waiting = 0;
938 cfs_waitq_broadcast(&scrub->os_thread.t_ctl_waitq);
941 RETURN(rc < 0 ? rc : ooc->ooc_cached_items);
944 static int osd_scrub_main(void *args)
947 struct osd_device *dev = (struct osd_device *)args;
948 struct osd_scrub *scrub = &dev->od_scrub;
949 struct ptlrpc_thread *thread = &scrub->os_thread;
950 struct super_block *sb = osd_sb(dev);
954 cfs_daemonize("OI_scrub");
955 rc = lu_env_init(&env, LCT_DT_THREAD);
957 CERROR("%.16s: OI scrub, fail to init env, rc = %d\n",
958 LDISKFS_SB(sb)->s_es->s_volume_name, rc);
962 rc = osd_scrub_prep(dev);
964 CERROR("%.16s: OI scrub, fail to scrub prep, rc = %d\n",
965 LDISKFS_SB(sb)->s_es->s_volume_name, rc);
969 if (!scrub->os_full_speed) {
970 struct l_wait_info lwi = { 0 };
971 struct osd_otable_it *it = dev->od_otable_it;
972 struct osd_otable_cache *ooc = &it->ooi_cache;
974 l_wait_event(thread->t_ctl_waitq,
975 it->ooi_user_ready || !thread_is_running(thread),
977 if (unlikely(!thread_is_running(thread)))
980 LASSERT(scrub->os_pos_current >= ooc->ooc_pos_preload);
981 scrub->os_pos_current = ooc->ooc_pos_preload;
984 CDEBUG(D_LFSCK, "OI scrub: flags = 0x%x, pos = %u\n",
985 scrub->os_start_flags, scrub->os_pos_current);
987 rc = osd_inode_iteration(osd_oti_get(&env), dev, ~0U, 0);
988 if (unlikely(rc == SCRUB_IT_CRASH))
989 GOTO(out, rc = -EINVAL);
993 osd_scrub_post(scrub, rc);
994 CDEBUG(D_LFSCK, "OI scrub: stop, rc = %d, pos = %u\n",
995 rc, scrub->os_pos_current);
998 while (!cfs_list_empty(&scrub->os_inconsistent_items)) {
999 struct osd_inconsistent_item *oii;
1001 oii = cfs_list_entry(scrub->os_inconsistent_items.next,
1002 struct osd_inconsistent_item, oii_list);
1003 cfs_list_del_init(&oii->oii_list);
1009 spin_lock(&scrub->os_lock);
1010 thread_set_flags(thread, SVC_STOPPED);
1011 cfs_waitq_broadcast(&thread->t_ctl_waitq);
1012 spin_unlock(&scrub->os_lock);
1016 /* initial OI scrub */
1018 typedef int (*scandir_t)(struct osd_thread_info *, struct osd_device *,
1019 struct dentry *, filldir_t filldir);
1021 static int osd_ios_varfid_fill(void *buf, const char *name, int namelen,
1022 loff_t offset, __u64 ino, unsigned d_type);
1025 osd_ios_general_scan(struct osd_thread_info *info, struct osd_device *dev,
1026 struct dentry *dentry, filldir_t filldir);
1028 osd_ios_ROOT_scan(struct osd_thread_info *info, struct osd_device *dev,
1029 struct dentry *dentry, filldir_t filldir);
1032 osd_ios_OBJECTS_scan(struct osd_thread_info *info, struct osd_device *dev,
1033 struct dentry *dentry, filldir_t filldir);
1036 OLF_SCAN_SUBITEMS = 0x0001,
1037 OLF_HIDE_FID = 0x0002,
1038 OLF_SHOW_NAME = 0x0004,
1043 struct lu_fid olm_fid;
1045 scandir_t olm_scandir;
1046 filldir_t olm_filldir;
1049 /* Add the new introduced local files in the list in the future. */
1050 static const struct osd_lf_map osd_lf_maps[] = {
1052 { CATLIST, { FID_SEQ_LOCAL_FILE, LLOG_CATALOGS_OID, 0 }, OLF_SHOW_NAME,
1056 { MOUNT_CONFIGS_DIR, { FID_SEQ_LOCAL_FILE, MGS_CONFIGS_OID, 0 },
1057 OLF_SCAN_SUBITEMS, osd_ios_general_scan,
1058 osd_ios_varfid_fill },
1060 /* NIDTBL_VERSIONS */
1061 { MGS_NIDTBL_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1062 osd_ios_general_scan, osd_ios_varfid_fill },
1065 { "PENDING", { FID_SEQ_LOCAL_FILE, MDD_ORPHAN_OID, 0 }, 0, NULL, NULL },
1068 { "ROOT", { FID_SEQ_LOCAL_FILE, MDD_ROOT_INDEX_OID, 0 },
1069 OLF_SCAN_SUBITEMS | OLF_HIDE_FID, osd_ios_ROOT_scan, NULL },
1072 { CAPA_KEYS, { FID_SEQ_LOCAL_FILE, MDD_CAPA_KEYS_OID, 0 }, 0,
1075 /* changelog_catalog */
1076 { CHANGELOG_CATALOG, { 0, 0, 0 }, 0, NULL, NULL },
1078 /* changelog_users */
1079 { CHANGELOG_USERS, { 0, 0, 0 }, 0, NULL, NULL },
1082 { "fld", { FID_SEQ_LOCAL_FILE, FLD_INDEX_OID, 0 }, OLF_SHOW_NAME,
1086 { LAST_RCVD, { FID_SEQ_LOCAL_FILE, LAST_RECV_OID, 0 }, OLF_SHOW_NAME,
1089 /* lfsck_bookmark */
1090 { "lfsck_bookmark", { FID_SEQ_LOCAL_FILE, LFSCK_BOOKMARK_OID, 0 }, 0,
1094 { LOV_OBJID, { FID_SEQ_LOCAL_FILE, MDD_LOV_OBJ_OID, 0 }, OLF_SHOW_NAME,
1098 { LOV_OBJSEQ, { FID_SEQ_LOCAL_FILE, MDD_LOV_OBJ_OSEQ, 0 },
1099 OLF_SHOW_NAME, NULL, NULL },
1102 { QMT_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1103 osd_ios_general_scan, osd_ios_varfid_fill },
1106 { QSD_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1107 osd_ios_general_scan, osd_ios_varfid_fill },
1109 /* seq-200000003-lastid */
1110 { "seq-200000003-lastid", { FID_SEQ_LOCAL_NAME, 1, 0 }, 0,
1114 { "seq_ctl", { FID_SEQ_LOCAL_FILE, FID_SEQ_CTL_OID, 0 },
1115 OLF_SHOW_NAME, NULL, NULL },
1118 { "seq_srv", { FID_SEQ_LOCAL_FILE, FID_SEQ_SRV_OID, 0 },
1119 OLF_SHOW_NAME, NULL, NULL },
1122 { "LAST_GROUP", { FID_SEQ_LOCAL_FILE, OFD_LAST_GROUP_OID, 0 },
1123 OLF_SHOW_NAME, NULL, NULL },
1126 { HEALTH_CHECK, { FID_SEQ_LOCAL_FILE, OFD_HEALTH_CHECK_OID, 0 },
1127 OLF_SHOW_NAME, NULL, NULL },
1129 /* OBJECTS, upgrade from old device */
1130 { OBJECTS, { 0, 0, 0 }, OLF_SCAN_SUBITEMS, osd_ios_OBJECTS_scan, NULL },
1132 /* lquota_v2.user, upgrade from old device */
1133 { "lquota_v2.user", { 0, 0, 0 }, 0, NULL, NULL },
1135 /* lquota_v2.group, upgrade from old device */
1136 { "lquota_v2.group", { 0, 0, 0 }, 0, NULL, NULL },
1138 { NULL, { 0, 0, 0 }, 0, NULL, NULL }
1141 struct osd_ios_item {
1142 cfs_list_t oii_list;
1143 struct dentry *oii_dentry;
1144 scandir_t oii_scandir;
1145 filldir_t oii_filldir;
1148 struct osd_ios_filldir_buf {
1149 struct osd_thread_info *oifb_info;
1150 struct osd_device *oifb_dev;
1151 struct dentry *oifb_dentry;
1154 static inline struct dentry *
1155 osd_ios_lookup_one_len(const char *name, struct dentry *parent, int namelen)
1157 struct dentry *dentry;
1159 dentry = ll_lookup_one_len(name, parent, namelen);
1160 if (!IS_ERR(dentry) && dentry->d_inode == NULL) {
1162 return ERR_PTR(-ENOENT);
1169 osd_ios_llogname2fid(struct lu_fid *fid, const char *name, int namelen)
1174 fid->f_seq = FID_SEQ_LLOG;
1176 id = id * 10 + name[i++] - '0';
1178 fid->f_oid = id & 0x00000000ffffffffULL;
1179 fid->f_ver = id >> 32;
1183 osd_ios_Oname2fid(struct lu_fid *fid, const char *name, int namelen)
1189 seq = seq * 10 + name[i++] - '0';
1191 lu_last_id_fid(fid, seq);
1195 osd_ios_new_item(struct osd_device *dev, struct dentry *dentry,
1196 scandir_t scandir, filldir_t filldir)
1198 struct osd_ios_item *item;
1200 OBD_ALLOC_PTR(item);
1204 CFS_INIT_LIST_HEAD(&item->oii_list);
1205 item->oii_dentry = dget(dentry);
1206 item->oii_scandir = scandir;
1207 item->oii_filldir = filldir;
1208 cfs_list_add_tail(&item->oii_list, &dev->od_ios_list);
1213 * osd_ios_scan_one() - check/fix LMA FID and OI entry for one inode
1215 * The passed \a inode's \a fid is verified against the LMA FID. If the \a fid
1216 * is NULL or is empty the IGIF FID is used. The FID is verified in the OI to
1217 * reference the inode, or fixed if it is missing or references another inode.
1220 osd_ios_scan_one(struct osd_thread_info *info, struct osd_device *dev,
1221 struct inode *inode, const struct lu_fid *fid, int flags)
1223 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
1224 struct osd_inode_id *id = &info->oti_id;
1225 struct osd_inode_id *id2 = &info->oti_id2;
1226 struct osd_scrub *scrub = &dev->od_scrub;
1227 struct scrub_file *sf = &scrub->os_file;
1232 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
1233 if (rc != 0 && rc != -ENODATA)
1236 osd_id_gen(id, inode->i_ino, inode->i_generation);
1237 if (rc == -ENODATA) {
1238 if (fid == NULL || fid_is_zero(fid) || flags & OLF_HIDE_FID)
1239 lu_igif_build(&tfid, inode->i_ino, inode->i_generation);
1242 rc = osd_ea_fid_set(info, inode, &tfid);
1246 tfid = lma->lma_self_fid;
1249 rc = __osd_oi_lookup(info, dev, &tfid, id2);
1254 rc = osd_scrub_refresh_mapping(info, dev, &tfid, id,
1259 if (osd_id_eq_strict(id, id2))
1262 if (!(sf->sf_flags & SF_INCONSISTENT)) {
1263 osd_scrub_file_reset(scrub,
1264 LDISKFS_SB(osd_sb(dev))->s_es->s_uuid,
1266 rc = osd_scrub_file_store(scrub);
1271 rc = osd_scrub_refresh_mapping(info, dev, &tfid, id, DTO_INDEX_UPDATE);
1276 static int osd_ios_varfid_fill(void *buf, const char *name, int namelen,
1277 loff_t offset, __u64 ino, unsigned d_type)
1279 struct osd_ios_filldir_buf *fill_buf = buf;
1280 struct osd_device *dev = fill_buf->oifb_dev;
1281 struct dentry *child;
1285 /* skip any '.' started names */
1289 child = osd_ios_lookup_one_len(name, fill_buf->oifb_dentry, namelen);
1291 RETURN(PTR_ERR(child));
1293 rc = osd_ios_scan_one(fill_buf->oifb_info, dev, child->d_inode,
1295 if (rc == 0 && S_ISDIR(child->d_inode->i_mode))
1296 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
1297 osd_ios_varfid_fill);
1303 static int osd_ios_root_fill(void *buf, const char *name, int namelen,
1304 loff_t offset, __u64 ino, unsigned d_type)
1306 struct osd_ios_filldir_buf *fill_buf = buf;
1307 struct osd_device *dev = fill_buf->oifb_dev;
1308 const struct osd_lf_map *map;
1309 struct dentry *child;
1313 /* skip any '.' started names */
1317 for (map = osd_lf_maps; map->olm_name != NULL; map++) {
1318 if (strlen(map->olm_name) != namelen)
1321 if (strncmp(map->olm_name, name, namelen) == 0)
1325 if (map->olm_name == NULL)
1328 child = osd_ios_lookup_one_len(name, fill_buf->oifb_dentry, namelen);
1330 RETURN(PTR_ERR(child));
1332 rc = osd_ios_scan_one(fill_buf->oifb_info, dev, child->d_inode,
1333 &map->olm_fid, map->olm_flags);
1334 if (rc == 0 && map->olm_flags & OLF_SCAN_SUBITEMS)
1335 rc = osd_ios_new_item(dev, child, map->olm_scandir,
1343 osd_ios_general_scan(struct osd_thread_info *info, struct osd_device *dev,
1344 struct dentry *dentry, filldir_t filldir)
1346 struct osd_ios_filldir_buf buf = { info, dev, dentry };
1347 struct file *filp = &info->oti_it_ea.oie_file;
1348 struct inode *inode = dentry->d_inode;
1349 const struct file_operations *fops = inode->i_fop;
1353 LASSERT(filldir != NULL);
1356 filp->f_dentry = dentry;
1357 filp->f_mode = FMODE_64BITHASH;
1358 filp->f_mapping = inode->i_mapping;
1360 filp->private_data = NULL;
1362 rc = fops->readdir(filp, &buf, filldir);
1363 fops->release(inode, filp);
1369 osd_ios_ROOT_scan(struct osd_thread_info *info, struct osd_device *dev,
1370 struct dentry *dentry, filldir_t filldir)
1372 struct osd_scrub *scrub = &dev->od_scrub;
1373 struct scrub_file *sf = &scrub->os_file;
1374 struct dentry *child;
1378 /* It is existing MDT device. */
1379 dev->od_handle_nolma = 1;
1380 child = osd_ios_lookup_one_len(dot_lustre_name, dentry,
1381 strlen(dot_lustre_name));
1382 if (IS_ERR(child)) {
1383 rc = PTR_ERR(child);
1384 if (rc == -ENOENT) {
1385 /* It is 1.8 MDT device. */
1386 if (!(sf->sf_flags & SF_UPGRADE)) {
1387 osd_scrub_file_reset(scrub,
1388 LDISKFS_SB(osd_sb(dev))->s_es->s_uuid,
1390 rc = osd_scrub_file_store(scrub);
1396 rc = osd_ios_scan_one(info, dev, child->d_inode, NULL, 0);
1404 osd_ios_OBJECTS_scan(struct osd_thread_info *info, struct osd_device *dev,
1405 struct dentry *dentry, filldir_t filldir)
1407 struct dentry *child;
1411 child = osd_ios_lookup_one_len(ADMIN_USR, dentry, strlen(ADMIN_USR));
1412 if (!IS_ERR(child)) {
1413 rc = osd_ios_scan_one(info, dev, child->d_inode, NULL, 0);
1416 rc = PTR_ERR(child);
1419 if (rc != 0 && rc != -ENOENT)
1422 child = osd_ios_lookup_one_len(ADMIN_GRP, dentry, strlen(ADMIN_GRP));
1423 if (!IS_ERR(child)) {
1424 rc = osd_ios_scan_one(info, dev, child->d_inode, NULL, 0);
1427 rc = PTR_ERR(child);
1436 static int osd_initial_OI_scrub(struct osd_thread_info *info,
1437 struct osd_device *dev)
1439 struct osd_ios_item *item = NULL;
1440 scandir_t scandir = osd_ios_general_scan;
1441 filldir_t filldir = osd_ios_root_fill;
1442 struct dentry *dentry = osd_sb(dev)->s_root;
1447 rc = scandir(info, dev, dentry, filldir);
1449 dput(item->oii_dentry);
1456 if (cfs_list_empty(&dev->od_ios_list))
1459 item = cfs_list_entry(dev->od_ios_list.next,
1460 struct osd_ios_item, oii_list);
1461 cfs_list_del_init(&item->oii_list);
1463 LASSERT(item->oii_scandir != NULL);
1464 scandir = item->oii_scandir;
1465 filldir = item->oii_filldir;
1466 dentry = item->oii_dentry;
1469 while (!cfs_list_empty(&dev->od_ios_list)) {
1470 item = cfs_list_entry(dev->od_ios_list.next,
1471 struct osd_ios_item, oii_list);
1472 cfs_list_del_init(&item->oii_list);
1473 dput(item->oii_dentry);
1480 char *osd_lf_fid2name(const struct lu_fid *fid)
1482 const struct osd_lf_map *map = osd_lf_maps;
1484 while (map->olm_name != NULL) {
1485 if (!lu_fid_eq(fid, &map->olm_fid)) {
1490 if (map->olm_flags & OLF_SHOW_NAME)
1491 return map->olm_name;
1499 /* OI scrub start/stop */
1501 static int do_osd_scrub_start(struct osd_device *dev, __u32 flags)
1503 struct osd_scrub *scrub = &dev->od_scrub;
1504 struct ptlrpc_thread *thread = &scrub->os_thread;
1505 struct l_wait_info lwi = { 0 };
1510 /* os_lock: sync status between stop and scrub thread */
1511 spin_lock(&scrub->os_lock);
1512 if (thread_is_running(thread)) {
1513 spin_unlock(&scrub->os_lock);
1515 } else if (unlikely(thread_is_stopping(thread))) {
1516 spin_unlock(&scrub->os_lock);
1517 l_wait_event(thread->t_ctl_waitq,
1518 thread_is_stopped(thread),
1522 spin_unlock(&scrub->os_lock);
1524 if (scrub->os_file.sf_status == SS_COMPLETED)
1527 scrub->os_start_flags = flags;
1528 thread_set_flags(thread, 0);
1529 rc = cfs_create_thread(osd_scrub_main, dev, 0);
1531 CERROR("%.16s: cannot start iteration thread, rc = %d\n",
1532 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, rc);
1536 l_wait_event(thread->t_ctl_waitq,
1537 thread_is_running(thread) || thread_is_stopped(thread),
1543 int osd_scrub_start(struct osd_device *dev)
1548 /* od_otable_mutex: prevent curcurrent start/stop */
1549 mutex_lock(&dev->od_otable_mutex);
1550 rc = do_osd_scrub_start(dev, SS_AUTO);
1551 mutex_unlock(&dev->od_otable_mutex);
1553 RETURN(rc == -EALREADY ? 0 : rc);
1556 static void do_osd_scrub_stop(struct osd_scrub *scrub)
1558 struct ptlrpc_thread *thread = &scrub->os_thread;
1559 struct l_wait_info lwi = { 0 };
1561 /* os_lock: sync status between stop and scrub thread */
1562 spin_lock(&scrub->os_lock);
1563 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
1564 thread_set_flags(thread, SVC_STOPPING);
1565 spin_unlock(&scrub->os_lock);
1566 cfs_waitq_broadcast(&thread->t_ctl_waitq);
1567 l_wait_event(thread->t_ctl_waitq,
1568 thread_is_stopped(thread),
1570 /* Do not skip the last lock/unlock, which can guarantee that
1571 * the caller cannot return until the OI scrub thread exit. */
1572 spin_lock(&scrub->os_lock);
1574 spin_unlock(&scrub->os_lock);
1577 static void osd_scrub_stop(struct osd_device *dev)
1579 /* od_otable_mutex: prevent curcurrent start/stop */
1580 mutex_lock(&dev->od_otable_mutex);
1581 dev->od_scrub.os_paused = 1;
1582 do_osd_scrub_stop(&dev->od_scrub);
1583 mutex_unlock(&dev->od_otable_mutex);
1586 /* OI scrub setup/cleanup */
1588 static const char osd_scrub_name[] = "OI_scrub";
1590 int osd_scrub_setup(const struct lu_env *env, struct osd_device *dev)
1592 struct osd_thread_info *info = osd_oti_get(env);
1593 struct osd_scrub *scrub = &dev->od_scrub;
1594 struct lvfs_run_ctxt *ctxt = &scrub->os_ctxt;
1595 struct scrub_file *sf = &scrub->os_file;
1596 struct super_block *sb = osd_sb(dev);
1597 struct ldiskfs_super_block *es = LDISKFS_SB(sb)->s_es;
1598 struct lvfs_run_ctxt saved;
1604 memset(scrub, 0, sizeof(*scrub));
1605 OBD_SET_CTXT_MAGIC(ctxt);
1606 ctxt->pwdmnt = dev->od_mnt;
1607 ctxt->pwd = dev->od_mnt->mnt_root;
1608 ctxt->fs = get_ds();
1610 cfs_waitq_init(&scrub->os_thread.t_ctl_waitq);
1611 init_rwsem(&scrub->os_rwsem);
1612 spin_lock_init(&scrub->os_lock);
1613 CFS_INIT_LIST_HEAD(&scrub->os_inconsistent_items);
1615 push_ctxt(&saved, ctxt, NULL);
1616 filp = filp_open(osd_scrub_name, O_RDWR | O_CREAT, 0644);
1618 RETURN(PTR_ERR(filp));
1620 scrub->os_inode = igrab(filp->f_dentry->d_inode);
1621 filp_close(filp, 0);
1622 pop_ctxt(&saved, ctxt, NULL);
1623 ldiskfs_set_inode_state(scrub->os_inode,
1624 LDISKFS_STATE_LUSTRE_NO_OI);
1626 rc = osd_scrub_file_load(scrub);
1627 if (rc == -ENOENT) {
1628 osd_scrub_file_init(scrub, es->s_uuid);
1630 } else if (rc != 0) {
1633 if (memcmp(sf->sf_uuid, es->s_uuid, 16) != 0) {
1634 osd_scrub_file_reset(scrub, es->s_uuid,SF_INCONSISTENT);
1636 } else if (sf->sf_status == SS_SCANNING) {
1637 sf->sf_status = SS_CRASHED;
1642 if (sf->sf_pos_last_checkpoint != 0)
1643 scrub->os_pos_current = sf->sf_pos_last_checkpoint + 1;
1645 scrub->os_pos_current = LDISKFS_FIRST_INO(sb) + 1;
1648 rc = osd_scrub_file_store(scrub);
1653 /* Initialize OI files. */
1654 rc = osd_oi_init(info, dev);
1658 rc = osd_initial_OI_scrub(info, dev);
1659 if (rc == 0 && !dev->od_noscrub &&
1660 ((sf->sf_status == SS_PAUSED) ||
1661 (sf->sf_status == SS_CRASHED &&
1662 sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT | SF_UPGRADE |
1664 (sf->sf_status == SS_INIT &&
1665 sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT | SF_UPGRADE))))
1666 rc = osd_scrub_start(dev);
1671 void osd_scrub_cleanup(const struct lu_env *env, struct osd_device *dev)
1673 struct osd_scrub *scrub = &dev->od_scrub;
1675 LASSERT(dev->od_otable_it == NULL);
1677 if (scrub->os_inode != NULL) {
1678 osd_scrub_stop(dev);
1679 iput(scrub->os_inode);
1680 scrub->os_inode = NULL;
1682 if (dev->od_oi_table != NULL)
1683 osd_oi_fini(osd_oti_get(env), dev);
1686 /* object table based iteration APIs */
1688 static struct dt_it *osd_otable_it_init(const struct lu_env *env,
1689 struct dt_object *dt, __u32 attr,
1690 struct lustre_capa *capa)
1692 enum dt_otable_it_flags flags = attr >> DT_OTABLE_IT_FLAGS_SHIFT;
1693 enum dt_otable_it_valid valid = attr & ~DT_OTABLE_IT_FLAGS_MASK;
1694 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1695 struct osd_scrub *scrub = &dev->od_scrub;
1696 struct osd_otable_it *it;
1701 /* od_otable_mutex: prevent curcurrent init/fini */
1702 mutex_lock(&dev->od_otable_mutex);
1703 if (dev->od_otable_it != NULL)
1704 GOTO(out, it = ERR_PTR(-EALREADY));
1708 GOTO(out, it = ERR_PTR(-ENOMEM));
1710 dev->od_otable_it = it;
1712 it->ooi_cache.ooc_consumer_idx = -1;
1713 if (flags & DOIF_OUTUSED)
1714 it->ooi_used_outside = 1;
1716 if (flags & DOIF_RESET)
1719 if (valid & DOIV_ERROR_HANDLE) {
1720 if (flags & DOIF_FAILOUT)
1721 start |= SS_SET_FAILOUT;
1723 start |= SS_CLEAR_FAILOUT;
1726 rc = do_osd_scrub_start(dev, start);
1727 if (rc == -EALREADY) {
1728 it->ooi_cache.ooc_pos_preload = scrub->os_pos_current - 1;
1729 } else if (rc < 0) {
1730 dev->od_otable_it = NULL;
1732 GOTO(out, it = ERR_PTR(-EALREADY));
1734 it->ooi_cache.ooc_pos_preload = scrub->os_pos_current;
1740 mutex_unlock(&dev->od_otable_mutex);
1741 return (struct dt_it *)it;
1744 static void osd_otable_it_fini(const struct lu_env *env, struct dt_it *di)
1746 struct osd_otable_it *it = (struct osd_otable_it *)di;
1747 struct osd_device *dev = it->ooi_dev;
1749 /* od_otable_mutex: prevent curcurrent init/fini */
1750 mutex_lock(&dev->od_otable_mutex);
1751 do_osd_scrub_stop(&dev->od_scrub);
1752 LASSERT(dev->od_otable_it == it);
1754 dev->od_otable_it = NULL;
1755 mutex_unlock(&dev->od_otable_mutex);
1760 * XXX: Temporary used to notify otable iteration to be paused.
1762 static void osd_otable_it_put(const struct lu_env *env, struct dt_it *di)
1764 struct osd_device *dev = ((struct osd_otable_it *)di)->ooi_dev;
1766 /* od_otable_mutex: prevent curcurrent init/fini */
1767 mutex_lock(&dev->od_otable_mutex);
1768 dev->od_scrub.os_paused = 1;
1769 mutex_unlock(&dev->od_otable_mutex);
1773 * Set the OSD layer iteration start position as the specified key.
1775 * The LFSCK out of OSD layer does not know the detail of the key, so if there
1776 * are several keys, they cannot be compared out of OSD, so call "::get()" for
1777 * each key, and OSD will select the smallest one by itself.
1779 static int osd_otable_it_get(const struct lu_env *env,
1780 struct dt_it *di, const struct dt_key *key)
1782 struct osd_otable_it *it = (struct osd_otable_it *)di;
1783 struct osd_otable_cache *ooc = &it->ooi_cache;
1784 const char *str = (const char *)key;
1788 /* Forbid to set iteration position after iteration started. */
1789 if (it->ooi_user_ready)
1795 if (sscanf(str, "%u", &ino) <= 0)
1798 /* Skip the one that has been processed last time. */
1799 if (ooc->ooc_pos_preload > ++ino)
1800 ooc->ooc_pos_preload = ino;
1805 static int osd_otable_it_next(const struct lu_env *env, struct dt_it *di)
1807 struct osd_otable_it *it = (struct osd_otable_it *)di;
1808 struct osd_device *dev = it->ooi_dev;
1809 struct osd_scrub *scrub = &dev->od_scrub;
1810 struct osd_otable_cache *ooc = &it->ooi_cache;
1811 struct ptlrpc_thread *thread = &scrub->os_thread;
1812 struct l_wait_info lwi = { 0 };
1816 LASSERT(it->ooi_user_ready);
1819 if (!thread_is_running(thread) && !it->ooi_used_outside)
1822 if (ooc->ooc_cached_items > 0) {
1823 ooc->ooc_cached_items--;
1824 ooc->ooc_consumer_idx = (ooc->ooc_consumer_idx + 1) &
1825 ~OSD_OTABLE_IT_CACHE_MASK;
1829 if (it->ooi_all_cached) {
1830 l_wait_event(thread->t_ctl_waitq,
1831 !thread_is_running(thread),
1836 it->ooi_waiting = 1;
1837 l_wait_event(thread->t_ctl_waitq,
1838 ooc->ooc_pos_preload < scrub->os_pos_current ||
1839 !thread_is_running(thread),
1841 it->ooi_waiting = 0;
1843 if (!thread_is_running(thread) && !it->ooi_used_outside)
1846 rc = osd_otable_it_preload(env, it);
1853 static struct dt_key *osd_otable_it_key(const struct lu_env *env,
1854 const struct dt_it *di)
1856 struct osd_otable_it *it = (struct osd_otable_it *)di;
1857 struct osd_otable_cache *ooc = &it->ooi_cache;
1859 sprintf(it->ooi_key, "%u",
1860 ooc->ooc_cache[ooc->ooc_consumer_idx].oic_lid.oii_ino);
1861 return (struct dt_key *)it->ooi_key;
1864 static int osd_otable_it_key_size(const struct lu_env *env,
1865 const struct dt_it *di)
1867 return sizeof(((struct osd_otable_it *)di)->ooi_key);
1870 static int osd_otable_it_rec(const struct lu_env *env, const struct dt_it *di,
1871 struct dt_rec *rec, __u32 attr)
1873 struct osd_otable_it *it = (struct osd_otable_it *)di;
1874 struct osd_otable_cache *ooc = &it->ooi_cache;
1876 *(struct lu_fid *)rec = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_fid;
1880 static int osd_otable_it_load(const struct lu_env *env,
1881 const struct dt_it *di, __u64 hash)
1883 struct osd_otable_it *it = (struct osd_otable_it *)di;
1884 struct osd_device *dev = it->ooi_dev;
1885 struct osd_otable_cache *ooc = &it->ooi_cache;
1886 struct osd_scrub *scrub = &dev->od_scrub;
1888 if (it->ooi_user_ready)
1891 if (ooc->ooc_pos_preload <= LDISKFS_FIRST_INO(osd_sb(dev)))
1892 ooc->ooc_pos_preload = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
1893 it->ooi_user_ready = 1;
1894 if (!scrub->os_full_speed)
1895 cfs_waitq_broadcast(&scrub->os_thread.t_ctl_waitq);
1897 /* Unplug OSD layer iteration by the first next() call. */
1898 return osd_otable_it_next(env, (struct dt_it *)it);
1901 const struct dt_index_operations osd_otable_ops = {
1903 .init = osd_otable_it_init,
1904 .fini = osd_otable_it_fini,
1905 .put = osd_otable_it_put,
1906 .get = osd_otable_it_get,
1907 .next = osd_otable_it_next,
1908 .key = osd_otable_it_key,
1909 .key_size = osd_otable_it_key_size,
1910 .rec = osd_otable_it_rec,
1911 .load = osd_otable_it_load,
1915 /* high priority inconsistent items list APIs */
1917 int osd_oii_insert(struct osd_device *dev, struct osd_idmap_cache *oic,
1920 struct osd_inconsistent_item *oii;
1921 struct osd_scrub *scrub = &dev->od_scrub;
1922 struct ptlrpc_thread *thread = &scrub->os_thread;
1927 if (unlikely(oii == NULL))
1930 CFS_INIT_LIST_HEAD(&oii->oii_list);
1931 oii->oii_cache = *oic;
1932 oii->oii_insert = insert;
1934 spin_lock(&scrub->os_lock);
1935 if (unlikely(!thread_is_running(thread))) {
1936 spin_unlock(&scrub->os_lock);
1941 if (cfs_list_empty(&scrub->os_inconsistent_items))
1943 cfs_list_add_tail(&oii->oii_list, &scrub->os_inconsistent_items);
1944 spin_unlock(&scrub->os_lock);
1947 cfs_waitq_broadcast(&thread->t_ctl_waitq);
1952 int osd_oii_lookup(struct osd_device *dev, const struct lu_fid *fid,
1953 struct osd_inode_id *id)
1955 struct osd_scrub *scrub = &dev->od_scrub;
1956 struct osd_inconsistent_item *oii;
1959 spin_lock(&scrub->os_lock);
1960 cfs_list_for_each_entry(oii, &scrub->os_inconsistent_items, oii_list) {
1961 if (lu_fid_eq(fid, &oii->oii_cache.oic_fid)) {
1962 *id = oii->oii_cache.oic_lid;
1963 spin_unlock(&scrub->os_lock);
1967 spin_unlock(&scrub->os_lock);
1974 static const char *scrub_status_names[] = {
1985 static const char *scrub_flags_names[] = {
1993 static const char *scrub_param_names[] = {
1998 static int scrub_bits_dump(char **buf, int *len, int bits, const char *names[],
2006 rc = snprintf(*buf, *len, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
2012 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
2015 rc = snprintf(*buf, *len, "%s%c", names[i],
2016 bits != 0 ? ',' : '\n');
2027 static int scrub_time_dump(char **buf, int *len, __u64 time, const char *prefix)
2032 rc = snprintf(*buf, *len, "%s: "LPU64" seconds\n", prefix,
2033 cfs_time_current_sec() - time);
2035 rc = snprintf(*buf, *len, "%s: N/A\n", prefix);
2044 static int scrub_pos_dump(char **buf, int *len, __u64 pos, const char *prefix)
2049 rc = snprintf(*buf, *len, "%s: "LPU64"\n", prefix, pos);
2051 rc = snprintf(*buf, *len, "%s: N/A\n", prefix);
2060 int osd_scrub_dump(struct osd_device *dev, char *buf, int len)
2062 struct osd_scrub *scrub = &dev->od_scrub;
2063 struct scrub_file *sf = &scrub->os_file;
2070 down_read(&scrub->os_rwsem);
2071 rc = snprintf(buf, len,
2076 sf->sf_magic, (int)sf->sf_oi_count,
2077 scrub_status_names[sf->sf_status]);
2083 rc = scrub_bits_dump(&buf, &len, sf->sf_flags, scrub_flags_names,
2088 rc = scrub_bits_dump(&buf, &len, sf->sf_param, scrub_param_names,
2093 rc = scrub_time_dump(&buf, &len, sf->sf_time_last_complete,
2094 "time_since_last_completed");
2098 rc = scrub_time_dump(&buf, &len, sf->sf_time_latest_start,
2099 "time_since_latest_start");
2103 rc = scrub_time_dump(&buf, &len, sf->sf_time_last_checkpoint,
2104 "time_since_last_checkpoint");
2108 rc = scrub_pos_dump(&buf, &len, sf->sf_pos_latest_start,
2109 "latest_start_position");
2113 rc = scrub_pos_dump(&buf, &len, sf->sf_pos_last_checkpoint,
2114 "last_checkpoint_position");
2118 rc = scrub_pos_dump(&buf, &len, sf->sf_pos_first_inconsistent,
2119 "first_failure_position");
2123 checked = sf->sf_items_checked + scrub->os_new_checked;
2124 rc = snprintf(buf, len,
2125 "checked: "LPU64"\n"
2126 "updated: "LPU64"\n"
2128 "prior_updated: "LPU64"\n"
2129 "noscrub: "LPU64"\n"
2131 "success_count: %u\n",
2132 checked, sf->sf_items_updated, sf->sf_items_failed,
2133 sf->sf_items_updated_prior, sf->sf_items_noscrub,
2134 sf->sf_items_igif, sf->sf_success_count);
2141 if (thread_is_running(&scrub->os_thread)) {
2142 cfs_duration_t duration = cfs_time_current() -
2143 scrub->os_time_last_checkpoint;
2144 __u64 new_checked = scrub->os_new_checked * CFS_HZ;
2145 __u32 rtime = sf->sf_run_time +
2146 cfs_duration_sec(duration + HALF_SEC);
2149 do_div(new_checked, duration);
2151 do_div(speed, rtime);
2152 rc = snprintf(buf, len,
2153 "run_time: %u seconds\n"
2154 "average_speed: "LPU64" objects/sec\n"
2155 "real-time_speed: "LPU64" objects/sec\n"
2156 "current_position: %u\n",
2157 rtime, speed, new_checked, scrub->os_pos_current);
2159 if (sf->sf_run_time != 0)
2160 do_div(speed, sf->sf_run_time);
2161 rc = snprintf(buf, len,
2162 "run_time: %u seconds\n"
2163 "average_speed: "LPU64" objects/sec\n"
2164 "real-time_speed: N/A\n"
2165 "current_position: N/A\n",
2166 sf->sf_run_time, speed);
2176 up_read(&scrub->os_rwsem);