From 5e658549462bfcd0375760bb2cca69820932e1b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=B5=A9?= Date: Thu, 14 Aug 2025 09:41:06 +0800 Subject: [PATCH] =?UTF-8?q?add=20mount=20record=20Signed-off-by:=20?= =?UTF-8?q?=E6=9D=A8=E6=B5=A9=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- interfaces/innerkits/fs_manager/fstab_mount.c | 34 +++++++++++++++---- .../fstab_mount/include/fstab_mount_test.h | 2 ++ .../fstab_mount/src/fstab_mount_test.cpp | 18 ++++++++++ 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/interfaces/innerkits/fs_manager/fstab_mount.c b/interfaces/innerkits/fs_manager/fstab_mount.c index 078c1817d..0f777842b 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 72b16634a..a8885759e 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 55b474103..5039b9b8e 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 -- Gitee