fs/mnemofs: Fix open and rmdir return value and seek out of bounds.

- Update open return value to ENOTDIR when ancestor is a file.
- Update rmdir return value to ENOTDIR when path is not a directory.
- Better logs for file and dir operations.

Signed-off-by: Saurav Pal <resyfer.dev@gmail.com>
This commit is contained in:
Saurav Pal 2024-11-02 13:47:40 +00:00 committed by Xiang Xiao
parent a1bb967941
commit 4e53ee4486
4 changed files with 472 additions and 179 deletions

File diff suppressed because it is too large Load Diff

View File

@ -209,7 +209,7 @@ struct mfs_sb_s
/* This is for *dir VFS methods. */
struct mfs_fsdirent
struct mfs_fsdirent_s
{
struct fs_dirent_s base; /* VFS directory structure */
uint8_t idx; /* This only goes from 0 for ., 1 for .. and
@ -424,7 +424,8 @@ static inline mfs_t mfs_popcnt(mfs_t x)
#endif
}
static inline void MFS_EXTRA_LOG_DIRENT(FAR struct mfs_dirent_s *dirent)
static inline void MFS_EXTRA_LOG_DIRENT(FAR const struct mfs_dirent_s * const
dirent)
{
MFS_EXTRA_LOG("EXTRA_LOG_DIRENT", "Direntry details.");
MFS_EXTRA_LOG("EXTRA_LOG_DIRENT", "\tDirent location %p", dirent);
@ -441,6 +442,68 @@ static inline void MFS_EXTRA_LOG_DIRENT(FAR struct mfs_dirent_s *dirent)
/* TODO: Timespecs */
}
static inline void MFS_EXTRA_LOG_FSDIRENT(FAR const struct mfs_fsdirent_s *
const fsdirent)
{
MFS_EXTRA_LOG("EXTRA_LOG_FSDIRENT", "FS Direntry details.");
MFS_EXTRA_LOG("EXTRA_LOG_FSDIRENT", "\tDirentry depth %" PRIu32,
fsdirent->depth);
MFS_EXTRA_LOG("EXTRA_LOG_FSDIRENT", "\tRead index %" PRIu32,
fsdirent->idx);
MFS_EXTRA_LOG("EXTRA_LOG_FSDIRENT", "\tPath %p.", fsdirent->path);
MFS_EXTRA_LOG("EXTRA_LOG_FSDIRENT", "\tPitr %p.", fsdirent->pitr);
MFS_EXTRA_LOG("EXTRA_LOG_FSDIRENT", "\tDepth %" PRIu32, fsdirent->depth);
}
static inline void MFS_EXTRA_LOG_PITR(FAR const struct mfs_pitr_s * const
pitr)
{
MFS_EXTRA_LOG("EXTRA_LOG_PITR", "Pitr details.");
MFS_EXTRA_LOG("EXTRA_LOG_PITR", "\tDepth %" PRIu32, pitr->depth);
MFS_EXTRA_LOG("EXTRA_LOG_PITR", "\tCurrent Offset %" PRIu32, pitr->c_off);
MFS_EXTRA_LOG("EXTRA_LOG_PITR", "\tParent CTZ (%" PRIu32 ", %" PRIu32 ")",
pitr->p.ctz.idx_e, pitr->p.ctz.pg_e);
MFS_EXTRA_LOG("EXTRA_LOG_PITR", "\tParent Size %" PRIu32, pitr->p.sz);
MFS_EXTRA_LOG("EXTRA_LOG_PITR", "\tParent Offset %" PRIu32, pitr->p.off);
}
static inline void MFS_EXTRA_LOG_MN(FAR const struct mfs_mn_s * const mn)
{
MFS_EXTRA_LOG("EXTRA_LOG_MN", "Master node details.");
MFS_EXTRA_LOG("EXTRA_LOG_MN", "\tFirst journal block is %" PRIu32,
mn->jrnl_blk);
MFS_EXTRA_LOG("EXTRA_LOG_MN", "\tNext MN entry index %" PRIu32,
mn->mblk_idx);
MFS_EXTRA_LOG("EXTRA_LOG_MN", "\tRoot CTZ (%" PRIu32 ", %" PRIu32 ")",
mn->root_ctz.idx_e, mn->root_ctz.pg_e);
MFS_EXTRA_LOG("EXTRA_LOG_MN", "\tRoot Size %" PRIu32, mn->root_sz);
MFS_EXTRA_LOG("EXTRA_LOG_MN", "\tRoot Mode is %u.", mn->root_mode);
/* TODO: Timespecs */
}
static inline void MFS_EXTRA_LOG_F(FAR struct mfs_ofd_s *f)
{
MFS_EXTRA_LOG("EXTRA_LOG_F", "File structure details.");
MFS_EXTRA_LOG("EXTRA_LOG_F", "\tList details.");
MFS_EXTRA_LOG("EXTRA_LOG_F", "\t\tPrevious node %p.", f->list.prev);
MFS_EXTRA_LOG("EXTRA_LOG_F", "\t\tNext node %p.", f->list.next);
MFS_EXTRA_LOG("EXTRA_LOG_F", "\tCommon structure details.");
MFS_EXTRA_LOG("EXTRA_LOG_F", "\t\tDepth: %" PRIu32, f->com->depth);
MFS_EXTRA_LOG("EXTRA_LOG_F", "\t\tNew Entry: %s.",
f->com->new_ent ? "true" : "false");
MFS_EXTRA_LOG("EXTRA_LOG_F", "\t\tOffset: %" PRIu32, f->com->off);
MFS_EXTRA_LOG("EXTRA_LOG_F", "\t\tFlags: 0x%x.", f->com->oflags);
MFS_EXTRA_LOG("EXTRA_LOG_F", "\t\tReference Counter: %" PRIu8,
f->com->refcount);
MFS_EXTRA_LOG("EXTRA_LOG_F", "\t\tFile Size: %" PRIu32, f->com->sz);
MFS_EXTRA_LOG("EXTRA_LOG_F", "\t\tPath details.");
MFS_EXTRA_LOG("EXTRA_LOG_F", "\t\t\tOffset: %" PRIu32, f->com->path->off);
MFS_EXTRA_LOG("EXTRA_LOG_F", "\t\t\tSize: %" PRIu32, f->com->path->sz);
MFS_EXTRA_LOG("EXTRA_LOG_F", "\t\t\tCTZ (%" PRIu32 ", %" PRIu32 ").",
f->com->path->ctz.idx_e, f->com->path->ctz.pg_e);
}
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
@ -1408,10 +1471,10 @@ int mfs_lru_rdfromoff(FAR const struct mfs_sb_s * const sb,
FAR char *buf, const mfs_t buflen);
/****************************************************************************
* Name: mfs_lru_updatedinfo
* Name: mfs_lru_getupdatedinfo
*
* Description:
* Update information of the path.
* Update information of the path from the LRU.
*
* Input Parameters:
* sb - Superblock instance of the device.
@ -1423,9 +1486,9 @@ int mfs_lru_rdfromoff(FAR const struct mfs_sb_s * const sb,
*
****************************************************************************/
int mfs_lru_updatedinfo(FAR const struct mfs_sb_s * const sb,
FAR struct mfs_path_s * const path,
const mfs_t depth);
int mfs_lru_getupdatedinfo(FAR const struct mfs_sb_s * const sb,
FAR struct mfs_path_s * const path,
const mfs_t depth);
/****************************************************************************
* Name: mfs_lru_updatectz

View File

@ -491,6 +491,8 @@ int mfs_pitr_rm(FAR struct mfs_sb_s * const sb,
struct mfs_pitr_s pitr;
FAR struct mfs_dirent_s *dirent = NULL;
/* TODO: MFS_LOG */
mfs_pitr_init(sb, path, depth, &pitr, true);
mfs_pitr_readdirent(sb, path, &pitr, &dirent);
@ -559,8 +561,8 @@ errout:
void mfs_pitr_free(FAR const struct mfs_pitr_s * const pitr)
{
finfo("Pitr at depth %u with CTZ (%u, %u) freed.",
pitr->depth, pitr->p.ctz.idx_e, pitr->p.ctz.pg_e);
MFS_EXTRA_LOG("MFS_PITR_FREE", "Parent iterator at %p freed.", pitr);
MFS_EXTRA_LOG_PITR(pitr);
}
void mfs_pitr_adv_off(FAR struct mfs_pitr_s * const pitr,
@ -587,10 +589,10 @@ void mfs_pitr_adv_tochild(FAR struct mfs_pitr_s * const pitr,
{
/* (pitr->depth + 1) - 1 is the child's index. */
MFS_EXTRA_LOG("MFS_PITR_ADV_TOCHILD", "Advance pitr to child's offset.");
MFS_EXTRA_LOG_PITR(pitr);
MFS_EXTRA_LOG("MFS_PITR_ADV_TOCHILD", "New offset %" PRIu32, pitr->c_off);
pitr->c_off = path[pitr->depth].off;
finfo("Pitr at depth %u with CTZ (%u, %u) advanced to %u offset.",
pitr->depth, pitr->p.ctz.idx_e, pitr->p.ctz.pg_e, pitr->c_off);
}
int mfs_pitr_readdirent(FAR const struct mfs_sb_s * const sb,
@ -749,7 +751,7 @@ static int search_ctz_by_name(FAR const struct mfs_sb_s * const sb,
name_hash = mfs_hash(name, namelen);
ret = mfs_lru_updatedinfo(sb, path, depth);
ret = mfs_lru_getupdatedinfo(sb, path, depth);
if (predict_false(ret < 0))
{
goto errout;
@ -989,6 +991,7 @@ errout:
void mfs_free_patharr(FAR struct mfs_path_s *path)
{
MFS_EXTRA_LOG("MFS_FREE_PATHARR", "Path array at %p freed.", path);
fs_heap_free(path);
}

View File

@ -949,9 +949,9 @@ errout:
return ret;
}
int mfs_lru_updatedinfo(FAR const struct mfs_sb_s * const sb,
FAR struct mfs_path_s * const path,
const mfs_t depth)
int mfs_lru_getupdatedinfo(FAR const struct mfs_sb_s * const sb,
FAR struct mfs_path_s * const path,
const mfs_t depth)
{
int ret = OK;
bool found;