diff --git a/interfaces/innerkits/fs_manager/fstab_mount.c b/interfaces/innerkits/fs_manager/fstab_mount.c index 078c1817d1f2603c68951dde3e825e4fbabf180c..0f777842bf71464f625385ead7fce13bce204e45 100755 --- a/interfaces/innerkits/fs_manager/fstab_mount.c +++ b/interfaces/innerkits/fs_manager/fstab_mount.c @@ -48,8 +48,9 @@ extern "C" { #define FS_MANAGER_BUFFER_SIZE 512 #define BLOCK_SIZE_BUFFER (64) #define RESIZE_BUFFER_SIZE 1024 -#define MAX_GCALLOWNANCE 100 -#define GCALLOWANCE_INCREACE 10 +#define MAX_GC_ALLOWANCE 100 +#define GCALLOWANCE_INCREASE 10 +#define RECORD_MOUNT_FILE STARTUP_INIT_UT_PATH"/metadata/vab_log/init_bi" const off_t PARTITION_ACTIVE_SLOT_OFFSET = 1024; const off_t PARTITION_ACTIVE_SLOT_SIZE = 4; int g_bootSlots = -1; @@ -421,6 +422,22 @@ static int Mount(const char *source, const char *target, const char *fsType, return rc; } +INIT_STATIC int RecordMountFailed(int rc, FILE** file, int count) +{ + BEGET_CHECK(rc != 0, return 0); + BEGET_CHECK(file != NULL, return -1); + if (*file == NULL) { + *file = fopen(RECORD_MOUNT_FILE, "w"); + BEGET_ERROR_CHECK(*file != NULL, return -1, "Failed Open MountRecord File"); + } + char mountInfo[MAX_BUFFER_LEN] = {0}; + int bytes = snprintf_s(mountInfo, MAX_BUFFER_LEN, MAX_BUFFER_LEN - 1, "mount_retry:%d\n", count); + BEGET_ERROR_CHECK(bytes > 0, return -1, "Build MountInfo failed"); + size_t written = fwrite(mountInfo, 1, bytes, *file); + BEGET_CHECK_ONLY_ELOG(written == bytes, "Write Log failed %zu", written); + return 0; +} + INIT_STATIC int MountWithCheckpoint(const char *source, const char *target, const char *fsType, unsigned long flags, const char *data) { @@ -440,6 +457,8 @@ INIT_STATIC int MountWithCheckpoint(const char *source, const char *target, cons } int gcAllowance = 0; + int saveErrno = 0; + FILE *file = NULL; do { char realData[FS_MANAGER_BUFFER_SIZE] = {0}; int bytes = snprintf_s(realData, FS_MANAGER_BUFFER_SIZE, FS_MANAGER_BUFFER_SIZE - 1, "%s,%s:%d%%", @@ -450,13 +469,14 @@ INIT_STATIC int MountWithCheckpoint(const char *source, const char *target, cons } rc = mount(source, target, fsType, flags, realData); BEGET_LOGI("MountWithCheckpoint %s %d %d", realData, rc, errno); - - if (rc != 0 && errno == EBUSY) { + saveErrno = errno; + if (rc != 0 && saveErrno == EBUSY) { rc = 0; } - gcAllowance += GCALLOWANCE_INCREACE; - } while (rc != 0 && errno == EAGAIN && gcAllowance <= MAX_GCALLOWNANCE); - + RecordMountFailed(rc, &file, (gcAllowance / GCALLOWANCE_INCREASE) + 1); + gcAllowance += GCALLOWANCE_INCREASE; + } while (rc != 0 && saveErrno == EAGAIN && gcAllowance <= MAX_GC_ALLOWANCE); + BEGET_ERROR_CHECK(file == NULL, fclose(file), "close file"); return rc; } diff --git a/test/unittest/single_test/fstab_mount/include/fstab_mount_test.h b/test/unittest/single_test/fstab_mount/include/fstab_mount_test.h index 72b16634a11d130afc2d9d7109504316f1789d80..a8885759eec09ad696141cd74b303631835c0536 100644 --- a/test/unittest/single_test/fstab_mount/include/fstab_mount_test.h +++ b/test/unittest/single_test/fstab_mount/include/fstab_mount_test.h @@ -29,6 +29,8 @@ int GetDataWithoutCheckpoint(char *fsSpecificData, size_t fsSpecificDataSize, int DoMountOneItem(FstabItem *item); +int RecordMountFailed(int rc, FILE** file, int count); + #ifdef __cplusplus } #endif diff --git a/test/unittest/single_test/fstab_mount/src/fstab_mount_test.cpp b/test/unittest/single_test/fstab_mount/src/fstab_mount_test.cpp index 55b47410392ba60f31ce386946b4245cdf8a941d..5039b9b8ed077791343204718138a4331dbe27cb 100644 --- a/test/unittest/single_test/fstab_mount/src/fstab_mount_test.cpp +++ b/test/unittest/single_test/fstab_mount/src/fstab_mount_test.cpp @@ -244,4 +244,22 @@ HWTEST_F(FstabMountTest, MountWithCheckpoint_003, TestSize.Level0) EXPECT_EQ(rc, 0); } +HWTEST_F(FstabMountTest, RecordMountFailed_001, TestSize.Level0) +{ + FILE *file = nullptr; + const char *fileName = STARTUP_INIT_UT_PATH"/metadata/vab_log/init_bi"; + CheckAndCreateDir(fileName); + int rc = RecordMountFailed(0, &file, 1); + EXPECT_EQ(rc, 0); + + rc = RecordMountFailed(-1, &file, 1); + EXPECT_EQ(rc, 0); + + rc = RecordMountFailed(-1, &file, 2); + EXPECT_EQ(rc, 0); + if (file != nullptr) { + rc = fclose(file); + EXPECT_EQ(rc, 0); + } +} } \ No newline at end of file