From 6ba5d80f8cdb9b04605190984fe5fdb74a626ebe Mon Sep 17 00:00:00 2001 From: TaeJun Kwon Date: Wed, 14 Jun 2017 12:57:17 +0900 Subject: [PATCH] Fix the issue that root sector corrupted [Description] 1.firstsector references Journal manager buffer, so wrong value has inserted when buffer value changed 2. If there are duplicated logical sector, sometimes smart_scan chooses released physical sector. [Module] File System [Board] all [Verification] N/A [Reference] N/A [Author] tj80.kwon Change-Id: I60e528bf1493600846b2dd86c6cd32c3f09c5a57 --- os/fs/driver/mtd/smart.c | 12 +++++++++-- os/fs/smartfs/smartfs_utils.c | 46 ++++++++++++++++++++++++------------------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/os/fs/driver/mtd/smart.c b/os/fs/driver/mtd/smart.c index 283dc30..6887d30 100644 --- a/os/fs/driver/mtd/smart.c +++ b/os/fs/driver/mtd/smart.c @@ -1701,6 +1701,7 @@ static int smart_scan(FAR struct smart_struct_s *dev) uint16_t sectorsize, prerelease; uint16_t logicalsector; uint16_t loser; + uint16_t winner; uint32_t readaddress; uint32_t offset; uint16_t seq1; @@ -1817,6 +1818,7 @@ static int smart_scan(FAR struct smart_struct_s *dev) } for (sector = 0; sector < totalsectors; sector++) { + winner = sector; fvdbg("Scan sector %d\n", sector); /* Calculate the read address for this sector */ @@ -2115,10 +2117,16 @@ static int smart_scan(FAR struct smart_struct_s *dev) #else loser = dupsector; #endif + winner = sector; } else { /* We keep the original mapping and seq2 is the loser */ loser = sector; +#ifndef CONFIG_MTD_SMART_MINIMIZE_RAM + winner = dev->sMap[logicalsector]; +#else + winner = smart_cache_lookup(dev, logicalsector); +#endif } /* Now release the loser sector */ @@ -2143,13 +2151,13 @@ static int smart_scan(FAR struct smart_struct_s *dev) #ifndef CONFIG_MTD_SMART_MINIMIZE_RAM /* Update the logical to physical sector map */ - dev->sMap[logicalsector] = sector; + dev->sMap[logicalsector] = winner; #else /* Mark the logical sector as used in the bitmap */ dev->sBitMap[logicalsector >> 3] |= 1 << (logicalsector & 0x07); if (logicalsector < dev->reservedsector) { - smart_add_sector_to_cache(dev, logicalsector, sector, __LINE__); + smart_add_sector_to_cache(dev, logicalsector, winner, __LINE__); } #endif } diff --git a/os/fs/smartfs/smartfs_utils.c b/os/fs/smartfs/smartfs_utils.c index 0ffe000..c4b5a51 100644 --- a/os/fs/smartfs/smartfs_utils.c +++ b/os/fs/smartfs/smartfs_utils.c @@ -2475,6 +2475,7 @@ static int smartfs_redo_rename(struct smartfs_mountpt_s *fs) char *filename; uint16_t oldflags; uint16_t oldoffset; + uint16_t firstsector; mode_t mode; uint16_t type; struct smart_read_write_s req; @@ -2504,35 +2505,40 @@ static int smartfs_redo_rename(struct smartfs_mountpt_s *fs) } direntry = (struct smartfs_entry_header_s *)&req.buffer[oldoffset]; - oldflags = direntry->flags; + if (ENTRY_VALID(direntry)) { + oldflags = direntry->flags; #ifdef CONFIG_SMARTFS_ALIGNED_ACCESS - type = (smartfs_rdle16(&oldflags) & SMARTFS_DIRENT_TYPE); - mode = (smartfs_rdle16(&oldflags) & SMARTFS_DIRENT_MODE); + type = (smartfs_rdle16(&oldflags) & SMARTFS_DIRENT_TYPE); + mode = (smartfs_rdle16(&oldflags) & SMARTFS_DIRENT_MODE); #else - type = oldflags & SMARTFS_DIRENT_TYPE; - mode = oldflags & SMARTFS_DIRENT_MODE; + type = oldflags & SMARTFS_DIRENT_TYPE; + mode = oldflags & SMARTFS_DIRENT_MODE; #endif - /* Now create new entry at new location */ - entry->generic_1 = (uint16_t)mode; - memcpy(j_mgr->buffer + sizeof(struct smartfs_logging_entry_s), filename, entry->datalen); - ret = smartfs_redo_create(fs, type, (uint16_t)direntry->firstsector); - if (ret != OK) { - goto errout; - } + firstsector = (uint16_t)direntry->firstsector; + /* Now create new entry at new location */ + entry->generic_1 = (uint16_t)mode; + memcpy(j_mgr->buffer + sizeof(struct smartfs_logging_entry_s), filename, entry->datalen); + ret = smartfs_redo_create(fs, type, firstsector); + if (ret != OK) { + goto errout; + } - /* Set old entry to inactive */ + /* Set old entry to inactive */ #if CONFIG_SMARTFS_ERASEDSTATE == 0xFF - oldflags &= ~SMARTFS_DIRENT_ACTIVE; + oldflags &= ~SMARTFS_DIRENT_ACTIVE; #else - oldflags |= SMARTFS_DIRENT_ACTIVE; + oldflags |= SMARTFS_DIRENT_ACTIVE; #endif - direntry->flags = oldflags; + direntry->flags = oldflags; - req.offset = oldoffset + offsetof(struct smartfs_entry_header_s, flags); - req.count = sizeof(direntry->flags); - req.buffer = (uint8_t *)&direntry->flags; - ret = FS_IOCTL(fs, BIOC_WRITESECT, (unsigned long)&req); + req.offset = oldoffset + offsetof(struct smartfs_entry_header_s, flags); + req.count = sizeof(direntry->flags); + req.buffer = (uint8_t *)&direntry->flags; + ret = FS_IOCTL(fs, BIOC_WRITESECT, (unsigned long)&req); + } else { + ret = OK; + } errout: if (filename) { kmm_free(filename); -- 2.7.4