Skip to content

Commit f9a3820

Browse files
committed
+Added FDB_TSDB_CTRL_SET_FORMAT_BAD_SEC_ONLY setting to TSDB to format only bad sectors to prevent data loss
1 parent b1043ed commit f9a3820

File tree

2 files changed

+53
-19
lines changed

2 files changed

+53
-19
lines changed

inc/fdb_def.h

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,25 @@ if (!(EXPR)) \
8484
#endif /* FDB_ASSERT */
8585
#endif /* FDB_USING_NATIVE_ASSERT */
8686

87-
#define FDB_KVDB_CTRL_SET_SEC_SIZE 0x00 /**< set sector size control command, this change MUST before database initialization */
88-
#define FDB_KVDB_CTRL_GET_SEC_SIZE 0x01 /**< get sector size control command */
89-
#define FDB_KVDB_CTRL_SET_LOCK 0x02 /**< set lock function control command */
90-
#define FDB_KVDB_CTRL_SET_UNLOCK 0x03 /**< set unlock function control command */
91-
#define FDB_KVDB_CTRL_SET_FILE_MODE 0x09 /**< set file mode control command, this change MUST before database initialization */
92-
#define FDB_KVDB_CTRL_SET_MAX_SIZE 0x0A /**< set database max size in file mode control command, this change MUST before database initialization */
93-
#define FDB_KVDB_CTRL_SET_NOT_FORMAT 0x0B /**< set database NOT format mode control command, this change MUST before database initialization */
94-
95-
#define FDB_TSDB_CTRL_SET_SEC_SIZE 0x00 /**< set sector size control command, this change MUST before database initialization */
96-
#define FDB_TSDB_CTRL_GET_SEC_SIZE 0x01 /**< get sector size control command */
97-
#define FDB_TSDB_CTRL_SET_LOCK 0x02 /**< set lock function control command */
98-
#define FDB_TSDB_CTRL_SET_UNLOCK 0x03 /**< set unlock function control command */
99-
#define FDB_TSDB_CTRL_SET_ROLLOVER 0x04 /**< set rollover control command, this change MUST after database initialization */
100-
#define FDB_TSDB_CTRL_GET_ROLLOVER 0x05 /**< get rollover control command */
101-
#define FDB_TSDB_CTRL_GET_LAST_TIME 0x06 /**< get last save time control command */
102-
#define FDB_TSDB_CTRL_SET_FILE_MODE 0x09 /**< set file mode control command, this change MUST before database initialization */
103-
#define FDB_TSDB_CTRL_SET_MAX_SIZE 0x0A /**< set database max size in file mode control command, this change MUST before database initialization */
104-
#define FDB_TSDB_CTRL_SET_NOT_FORMAT 0x0B /**< set database NOT formatable mode control command, this change MUST before database initialization */
87+
#define FDB_KVDB_CTRL_SET_SEC_SIZE 0x00 /**< set sector size control command, this change MUST before database initialization */
88+
#define FDB_KVDB_CTRL_GET_SEC_SIZE 0x01 /**< get sector size control command */
89+
#define FDB_KVDB_CTRL_SET_LOCK 0x02 /**< set lock function control command */
90+
#define FDB_KVDB_CTRL_SET_UNLOCK 0x03 /**< set unlock function control command */
91+
#define FDB_KVDB_CTRL_SET_FILE_MODE 0x09 /**< set file mode control command, this change MUST before database initialization */
92+
#define FDB_KVDB_CTRL_SET_MAX_SIZE 0x0A /**< set database max size in file mode control command, this change MUST before database initialization */
93+
#define FDB_KVDB_CTRL_SET_NOT_FORMAT 0x0B /**< set database NOT format mode control command, this change MUST before database initialization */
94+
95+
#define FDB_TSDB_CTRL_SET_SEC_SIZE 0x00 /**< set sector size control command, this change MUST before database initialization */
96+
#define FDB_TSDB_CTRL_GET_SEC_SIZE 0x01 /**< get sector size control command */
97+
#define FDB_TSDB_CTRL_SET_LOCK 0x02 /**< set lock function control command */
98+
#define FDB_TSDB_CTRL_SET_UNLOCK 0x03 /**< set unlock function control command */
99+
#define FDB_TSDB_CTRL_SET_ROLLOVER 0x04 /**< set rollover control command, this change MUST after database initialization */
100+
#define FDB_TSDB_CTRL_GET_ROLLOVER 0x05 /**< get rollover control command */
101+
#define FDB_TSDB_CTRL_GET_LAST_TIME 0x06 /**< get last save time control command */
102+
#define FDB_TSDB_CTRL_SET_FILE_MODE 0x09 /**< set file mode control command, this change MUST before database initialization */
103+
#define FDB_TSDB_CTRL_SET_MAX_SIZE 0x0A /**< set database max size in file mode control command, this change MUST before database initialization */
104+
#define FDB_TSDB_CTRL_SET_NOT_FORMAT 0x0B /**< set database NOT formatable mode control command, this change MUST before database initialization */
105+
#define FDB_TSDB_CTRL_SET_FORMAT_BAD_SEC_ONLY 0x0C /**< format bad sectors only */
105106

106107
#ifdef FDB_USING_TIMESTAMP_64BIT
107108
typedef int64_t fdb_time_t;
@@ -278,6 +279,7 @@ struct fdb_db {
278279
bool init_ok; /**< initialized successfully */
279280
bool file_mode; /**< is file mode, default is false */
280281
bool not_formatable; /**< is can NOT be formated mode, default is false */
282+
bool format_bad_sec_only; /**< only format the bad sectors */
281283
#ifdef FDB_USING_FILE_MODE
282284
uint32_t cur_file_sec[FDB_FILE_CACHE_TABLE_SIZE];/**< last operate sector address */
283285
#if defined(FDB_USING_FILE_POSIX_MODE)

src/fdb_tsdb.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,14 @@ static bool format_all_cb(tsdb_sec_info_t sector, void *arg1, void *arg2)
804804

805805
return false;
806806
}
807+
static bool format_cb(tsdb_sec_info_t sector, void *arg1, void *arg2)
808+
{
809+
fdb_tsdb_t db = arg1;
810+
811+
format_sector(db, sector->addr);
812+
813+
return true;
814+
}
807815

808816
static void tsl_format_all(fdb_tsdb_t db)
809817
{
@@ -906,6 +914,11 @@ void fdb_tsdb_control(fdb_tsdb_t db, int cmd, void *arg)
906914
FDB_ASSERT(db->parent.init_ok == false);
907915
db->parent.not_formatable = *(bool *)arg;
908916
break;
917+
case FDB_TSDB_CTRL_SET_FORMAT_BAD_SEC_ONLY:
918+
/* this change MUST before database initialization */
919+
FDB_ASSERT(db->parent.init_ok == false);
920+
db->parent.format_bad_sec_only = *(bool *)arg;
921+
break;
909922
}
910923
}
911924

@@ -948,7 +961,26 @@ fdb_err_t fdb_tsdb_init(fdb_tsdb_t db, const char *name, const char *path, fdb_g
948961

949962
/* check all sector header */
950963
sector.addr = 0;
951-
sector_iterator(db, &sector, FDB_SECTOR_STORE_UNUSED, &check_sec_arg, NULL, check_sec_hdr_cb, true);
964+
965+
if ((db->parent.not_formatable == false) && (db->parent.format_bad_sec_only)) {
966+
uint32_t addr = 0xFFFFFFFF;
967+
968+
do {
969+
check_sec_arg.check_failed = false;
970+
sector_iterator(db, &sector, FDB_SECTOR_STORE_UNUSED, &check_sec_arg, NULL, check_sec_hdr_cb, true);
971+
if (check_sec_arg.check_failed) {
972+
/* exit if sector has already been checked to prevent infinite loop */
973+
if (sector.addr == addr) {
974+
break;
975+
}
976+
sector_iterator(db, &sector, FDB_SECTOR_STORE_UNUSED, db, NULL, format_cb, false);
977+
addr = sector.addr;
978+
}
979+
} while (check_sec_arg.check_failed);
980+
} else {
981+
sector_iterator(db, &sector, FDB_SECTOR_STORE_UNUSED, &check_sec_arg, NULL, check_sec_hdr_cb, true);
982+
}
983+
952984
/* format all sector when check failed */
953985
if (check_sec_arg.check_failed) {
954986
if (db->parent.not_formatable) {

0 commit comments

Comments
 (0)