jingfei195887 opened a new pull request, #22541:
URL: https://github.com/apache/nuttx/pull/22541

   ## Summary
   
   With `CONFIG_FS_LITTLEFS_ATTR_UPDATE` disabled, `fstat()` succeeds for an 
open LittleFS regular file but leaves `st_mode` at zero, so `S_ISREG()` is 
false. Set the file type outside the optional attribute block. Stored 
permissions, ownership, timestamps, error handling and disk format retain their 
existing behavior.
   
   ## Impact
   
   - Feature: fixes regular-file type reporting when optional attributes are 
disabled.
   - User adaptation: none.
   - Build process / hardware / drivers: unchanged.
   - Documentation: describes type availability without optional attributes.
   - Security: no security claim.
   - Compatibility: no API, configuration or on-disk format change; successful 
`fstat()` now reports the expected type.
   
   ## Testing
   
   Tested locally on Linux x86-64, using sim:nsh with a real LittleFS instance 
on a 256 KiB RAM MTD device. The standalone apps test below covers empty and 
nonempty files, pending and synced writes, size/block accounting, duplicated 
descriptors, read-only/write-only/read-write opens, internal file API, rename, 
unlink, truncate, remount, stored permissions and invalid descriptors.
   
   | Configuration | Baseline | This PR |
   |---|---|---|
   | LittleFS v2.11.2, attributes off | 108 checks, 11 type failures | 108 
checks, 0 failures |
   | LittleFS v2.11.2, attributes on | 105 checks, 2 existing EFAULT failures | 
Identical 105 checks, same 2 failures |
   | 32-bit sim, attributes off, 512 KiB FS heap | Not run | 108 checks, 0 
failures |
   | LittleFS v2.11.1, no NuttX local patches | Not run | 108 checks, 0 
failures |
   
   The attributes-on run is **not** an all-passing runtime result: `fstat()` 
immediately after renaming or unlinking an open file returns `EFAULT` on both 
revisions. GDB localized the two failures to those calls; the baseline and 
branch runtime logs are byte-for-byte identical. This patch does not change 
optional-attribute lifetime handling. Both attributes-off/on comparison pairs 
use identical effective configurations and the same test source.
   
   Protected (`qemu-armv7r:pnsh`) and kernel (`qemu-armv8a:knsh`) images build 
with LittleFS enabled and attributes disabled; both images contain 
`littlefs_fstat`. sim Make/CMake builds and OSTest pass. No physical hardware, 
protected/kernel runtime, SMP stress, allocation-failure injection or 
power-loss tests were run.
   
   Baseline `f9d3eb13c3f412426a2658227f2b332e21e3a526`; tested head 
`71ddd2b6593c8279360904cde2ef67a6463251ca`. Optimized `DEFAULT_SMALL` sim 
configuration with LittleFS linked, attributes off and coverage disabled: text 
+0, data +0, bss +0 bytes.
   
   An initial dependency unpack/patch attempt failed before compilation; a 
fresh v2.11.2 archive accepted both stock patches and the clean build passed. 
That failed attempt is not counted. The first branch-off runtime completed 
successfully, then gcov reported stale host profile checksums at exit; its 
original output is retained below. Later runtimes use a separate GCOV_PREFIX 
per output directory. Footprint measurements disable coverage.
   
   <details>
   <summary>Before: attributes disabled</summary>
   
   
   ```text
   nsh> lfsstatcheck
   LFSSTAT attributes=off
   FAIL fstat regular file type errno=0
   FAIL fstat regular file type errno=0
   FAIL fstat regular file type errno=0
   FAIL fstat regular file type errno=0
   FAIL fstat regular file type errno=0
   FAIL fstat regular file type errno=0
   FAIL fstat regular file type errno=0
   FAIL internal regular file type errno=0
   FAIL fstat regular file type errno=0
   FAIL fstat regular file type errno=0
   FAIL fstat regular file type errno=0
   LFSSTAT RESULT checks=108 failures=11
   nsh> echo status=$?
   status=1
   nsh> poweroff
   ```
   
   </details>
   
   <details>
   <summary>After: attributes disabled</summary>
   
   
   ```text
   nsh> lfsstatcheck
   LFSSTAT attributes=off
   LFSSTAT RESULT checks=108 failures=0
   nsh> echo status=$?
   status=0
   nsh> poweroff
   libgcov profiling 
error:/home/mi/test/nuttx_apache/fs-upstream/apps/examples/lfsstatcheck/lfsstatcheck_main.c.home.mi.test.nuttx_apache.fs-upstream.apps.examples.lfsstatcheck.gcda:overwriting
 an existing profile data with a different checksum
   libgcov profiling 
error:/home/mi/test/nuttx_apache/fs-upstream/nuttx/fs/lfs_vfs.gcda:overwriting 
an existing profile data with a different checksum
   ```
   
   </details>
   
   <details>
   <summary>Before: attributes enabled (two existing errors)</summary>
   
   
   ```text
   nsh> lfsstatcheck
   LFSSTAT attributes=on
   FAIL fstat succeeds errno=14
   FAIL fstat succeeds errno=14
   LFSSTAT RESULT checks=105 failures=2
   nsh> echo status=$?
   status=1
   nsh> poweroff
   ```
   
   </details>
   
   <details>
   <summary>After: attributes enabled (same two existing errors)</summary>
   
   
   ```text
   nsh> lfsstatcheck
   LFSSTAT attributes=on
   FAIL fstat succeeds errno=14
   FAIL fstat succeeds errno=14
   LFSSTAT RESULT checks=105 failures=2
   nsh> echo status=$?
   status=1
   nsh> poweroff
   ```
   
   </details>
   
   <details>
   <summary>After: 32-bit and separate FS heap</summary>
   
   
   ```text
   nsh> lfsstatcheck
   LFSSTAT attributes=off
   LFSSTAT RESULT checks=108 failures=0
   nsh> echo status=$?
   status=0
   nsh> poweroff
   ```
   
   </details>
   
   <details>
   <summary>After: v2.11.1 without local patches</summary>
   
   
   ```text
   nsh> lfsstatcheck
   LFSSTAT attributes=off
   LFSSTAT RESULT checks=108 failures=0
   nsh> echo status=$?
   status=0
   nsh> poweroff
   ```
   
   </details>
   
   <details>
   <summary>Build log excerpts (before and after), modes and size</summary>
   
   
   baseline-off
   ```text
   CC:  sim/posix/sim_hostfs.c
   CC:  sim/posix/sim_errno.c
   CC:  sim/posix/sim_hostsmp.c
   CPP:  nuttx-names.in-> nuttx-names.dat 
   LD:  nuttx
   Pac SIM with dynamic libs..
   '/lib/x86_64-linux-gnu/libz.so.1' -> 'sim-pac/libs/libz.so.1'
   '/lib/x86_64-linux-gnu/libc.so.6' -> 'sim-pac/libs/libc.so.6'
   '/lib64/ld-linux-x86-64.so.2' -> 'sim-pac/ld-linux-x86-64.so.2'
   SIM elf with dynamic libs archive in nuttx.tgz
   ```
   
   branch-off
   ```text
   CC:  sim/posix/sim_hostfs.c
   CC:  sim/posix/sim_errno.c
   CC:  sim/posix/sim_hostsmp.c
   CPP:  nuttx-names.in-> nuttx-names.dat 
   LD:  nuttx
   Pac SIM with dynamic libs..
   '/lib/x86_64-linux-gnu/libz.so.1' -> 'sim-pac/libs/libz.so.1'
   '/lib/x86_64-linux-gnu/libc.so.6' -> 'sim-pac/libs/libc.so.6'
   '/lib64/ld-linux-x86-64.so.2' -> 'sim-pac/ld-linux-x86-64.so.2'
   SIM elf with dynamic libs archive in nuttx.tgz
   ```
   
   optimized-baseline
   ```text
   CC:  sim/posix/sim_hostfs.c
   CC:  sim/posix/sim_errno.c
   CC:  sim/posix/sim_hostsmp.c
   CPP:  nuttx-names.in-> nuttx-names.dat 
   LD:  nuttx
   Pac SIM with dynamic libs..
   '/lib/x86_64-linux-gnu/libz.so.1' -> 'sim-pac/libs/libz.so.1'
   '/lib/x86_64-linux-gnu/libc.so.6' -> 'sim-pac/libs/libc.so.6'
   '/lib64/ld-linux-x86-64.so.2' -> 'sim-pac/ld-linux-x86-64.so.2'
   SIM elf with dynamic libs archive in nuttx.tgz
   ```
   
   optimized-branch
   ```text
   CC:  sim/posix/sim_hostfs.c
   CC:  sim/posix/sim_errno.c
   CC:  sim/posix/sim_hostsmp.c
   CPP:  nuttx-names.in-> nuttx-names.dat 
   LD:  nuttx
   Pac SIM with dynamic libs..
   '/lib/x86_64-linux-gnu/libz.so.1' -> 'sim-pac/libs/libz.so.1'
   '/lib/x86_64-linux-gnu/libc.so.6' -> 'sim-pac/libs/libc.so.6'
   '/lib64/ld-linux-x86-64.so.2' -> 'sim-pac/ld-linux-x86-64.so.2'
   SIM elf with dynamic libs archive in nuttx.tgz
   ```
   
   protected
   ```text
   CPP:  
/home/mi/test/nuttx_apache/fs-upstream/nuttx/boards/arm/qemu/qemu-armv7r/scripts/nuttx.ld->
 
/home/mi/test/nuttx_apache/fs-upstream/nuttx/boards/arm/qemu/qemu-armv7r/scripts/nuttx.ld.tmp
 
   CC:  qemu_bringup.c 
   LD: nuttx
   arm-none-eabi-ld: warning: 
/home/mi/test/nuttx_apache/fs-upstream/nuttx/nuttx has a LOAD segment with RWX 
permissions
   Memory region         Used Size  Region Size  %age Used
             kflash:      197536 B       512 KB     37.68%
             uflash:          0 GB       512 KB      0.00%
              ksram:         12 KB         8 MB      0.15%
              usram:          0 GB         8 MB      0.00%
   CP: nuttx.bin
   ```
   
   kernel
   ```text
   CPP:  
/home/mi/test/nuttx_apache/fs-upstream/nuttx/boards/arm64/qemu/qemu-armv8a/scripts/ld-kernel.script->
 
/home/mi/test/nuttx_apache/fs-upstream/nuttx/boards/arm64/qemu/qemu-armv8a/scripts/ld-kernel.script.tmp
 
   CC:  qemu_boardinit.c 
   CC:  qemu_bringup.c 
   CC:  qemu_power.c 
   LD: nuttx
   Memory region         Used Size  Region Size  %age Used
               dram:        352 KB         2 MB     17.19%
              pgram:           0 B         4 MB      0.00%
   CP: nuttx.hex
   CP: nuttx.bin
   ```
   
   ```text
   qemu-armv7r:pnsh build PASS
   qemu-armv8a:knsh build PASS
   ```
   
   ```text
      text         data     bss     dec     hex filename
    606309         2800   13056  622165   97e55 nuttx
   ```
   
   ```text
      text         data     bss     dec     hex filename
    606309         2800   13056  622165   97e55 nuttx
   ```
   
   </details>
   
   <details>
   <summary>Make/CMake and OSTest verification</summary>
   
   
   ```text
   target   : nuttx
   branch   : fs/littlefs-fstat-type
   head     : 71ddd2b6593c8279360904cde2ef67a6463251ca
   subject  : fs/littlefs: report the file type without optional attributes
   commits  : 1
   base     : f9d3eb13c3 (merge-base with upstream/master)
   behind   : 0 commits of upstream/master
   nuttx    : 71ddd2b659
   apps     : 0a37af9866
   options  : CONFIG_FS_LITTLEFS CONFIG_FS_LITTLEFS_ATTR_UPDATE
   host     : Linux x86_64, gcc (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0
   checkpatch: rc=0
   signer   : jingfei <[email protected]>
   commits missing your Signed-off-by: 0
   make build: rc=0
   warnings in changed files: 0
   ostest: Exiting with status 0
   cmake reconfigure: rc=0
   cmake build: rc=0
   worktree tracked changes after: 0
   RESULT: PASS
   ```
   
   </details>
   
   <details>
   <summary>Reproducible standalone regression source</summary>
   
   Place the following files in `apps/examples/lfsstatcheck/`. Configure 
`sim:nsh`, enable `FS_LITTLEFS`, `MTD`, `RAMMTD`, `EXAMPLES_LFSSTATCHECK`, and 
`DEBUG_ASSERTIONS`; disable `FS_LITTLEFS_ATTR_UPDATE`. Run `make olddefconfig`, 
build, then run `lfsstatcheck` in NSH. Repeat with attributes enabled for the 
comparison above. This test is supplied here as a standalone local apps 
regression; no apps PR is included.
   
   Kconfig
   ```text
   config EXAMPLES_LFSSTATCHECK
        tristate "LittleFS fstat type regression"
        default n
        depends on FS_LITTLEFS && RAMMTD
   ```
   
   Make.defs
   ```text
   ifneq ($(CONFIG_EXAMPLES_LFSSTATCHECK),)
   CONFIGURED_APPS += $(APPDIR)/examples/lfsstatcheck
   endif
   ```
   
   Makefile
   ```text
   include $(APPDIR)/Make.defs
   PROGNAME = lfsstatcheck
   PRIORITY = SCHED_PRIORITY_DEFAULT
   STACKSIZE = 8192
   MODULE = $(CONFIG_EXAMPLES_LFSSTATCHECK)
   MAINSRC = lfsstatcheck_main.c
   include $(APPDIR)/Application.mk
   ```
   
   lfsstatcheck_main.c
   ```c
   /****************************************************************************
    * apps/examples/lfsstatcheck/lfsstatcheck_main.c
    *
    * SPDX-License-Identifier: Apache-2.0
    *
    * Licensed to the Apache Software Foundation (ASF) under one or more
    * contributor license agreements.  See the NOTICE file distributed with
    * this work for additional information regarding copyright ownership.  The
    * ASF licenses this file to you under the Apache License, Version 2.0 (the
    * "License"); you may not use this file except in compliance with the
    * License.  You may obtain a copy of the License at
    *
    *   http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
    * License for the specific language governing permissions and limitations
    * under the License.
    *
    
****************************************************************************/
   
   /****************************************************************************
    * Included Files
    
****************************************************************************/
   
   #include <nuttx/config.h>
   
   #include <sys/mount.h>
   #include <sys/stat.h>
   #include <stdbool.h>
   #include <stdint.h>
   #include <stdio.h>
   #include <string.h>
   #include <fcntl.h>
   #include <unistd.h>
   #include <errno.h>
   
   #include <nuttx/fs/fs.h>
   #include <nuttx/mtd/mtd.h>
   
   /****************************************************************************
    * Private Data
    
****************************************************************************/
   
   static const int g_flags[] =
   {
     O_RDONLY, O_WRONLY, O_RDWR
   };
   
   static uint8_t g_storage[256 * 1024];
   static char g_data[4097];
   static char g_readback[4097];
   static unsigned int g_checks;
   static unsigned int g_failures;
   
   /****************************************************************************
    * Private Functions
    
****************************************************************************/
   
   static void check(bool ok, FAR const char *name)
   {
     g_checks++;
     if (!ok)
       {
         g_failures++;
         printf("FAIL %s errno=%d\n", name, errno);
       }
   }
   
   static void check_fd(int fd, off_t size, mode_t mode)
   {
     struct stat st;
     int ret;
   
     memset(&st, 0xa5, sizeof(st));
     errno = 0;
     ret = fstat(fd, &st);
     check(ret == 0, "fstat succeeds");
     if (ret == 0)
       {
         check(S_ISREG(st.st_mode), "fstat regular file type");
         check(st.st_size == size, "fstat current size");
         check(st.st_blksize > 0, "fstat block size");
         check(st.st_blocks == (size + st.st_blksize - 1) / st.st_blksize,
               "fstat block count");
   #ifdef CONFIG_FS_LITTLEFS_ATTR_UPDATE
         check((st.st_mode & 0777) == mode, "fstat stored permissions");
   #else
         check((st.st_mode & 0777) == 0, "fstat permission behavior unchanged");
   #endif
       }
   }
   
   static void check_path(FAR const char *path, off_t size)
   {
     struct stat st;
     int ret = stat(path, &st);
   
     check(ret == 0, "stat succeeds");
     if (ret == 0)
       {
         check(S_ISREG(st.st_mode), "stat regular file type");
         check(st.st_size == size, "stat committed size");
       }
   }
   
   /****************************************************************************
    * Public Functions
    
****************************************************************************/
   
   int main(int argc, FAR char *argv[])
   {
     FAR struct mtd_dev_s *mtd;
     struct file filep;
     struct stat st;
     mode_t mode = 0640;
     mode_t saved_umask;
     int copy;
     int fd;
     int ret;
     unsigned int i;
   
     g_checks = 0;
     g_failures = 0;
     saved_umask = umask(0);
     memset(g_storage, 0xff, sizeof(g_storage));
     for (i = 0; i < sizeof(g_data); i++)
       {
         g_data[i] = (char)(i % 113);
       }
   
   #ifdef CONFIG_FS_LITTLEFS_ATTR_UPDATE
     printf("LFSSTAT attributes=on\n");
   #else
     printf("LFSSTAT attributes=off\n");
   #endif
     mtd = rammtd_initialize(g_storage, sizeof(g_storage));
     check(mtd != NULL, "RAM MTD initialize");
     if (mtd == NULL)
       {
         goto done;
       }
   
     ret = register_mtddriver("/dev/lfsstatmtd", mtd, 0666, NULL);
     check(ret == 0, "MTD register");
     if (ret < 0)
       {
         goto free_mtd;
       }
   
     ret = mount("/dev/lfsstatmtd", "/lfsstat", "littlefs", 0, "autoformat");
     check(ret == 0, "LittleFS format and mount");
     if (ret < 0)
       {
         goto unregister;
       }
   
     check(mkdir("/lfsstat/dir", 0777) == 0, "mkdir");
     check(stat("/lfsstat/dir", &st) == 0 && S_ISDIR(st.st_mode),
           "directory stat control");
     fd = open("/lfsstat/dir/file", O_CREAT | O_RDWR, mode);
     check(fd >= 0, "create empty file");
     if (fd < 0)
       {
         goto unmount;
       }
   
     check_fd(fd, 0, mode);
     check_path("/lfsstat/dir/file", 0);
     check(write(fd, g_data, sizeof(g_data)) == sizeof(g_data),
           "write payload");
     check_fd(fd, sizeof(g_data), mode);
     check(fsync(fd) == 0, "sync payload");
     check_path("/lfsstat/dir/file", sizeof(g_data));
     copy = dup(fd);
     check(copy >= 0, "dup");
     if (copy >= 0)
       {
         check_fd(copy, sizeof(g_data), mode);
         check(lseek(copy, 0, SEEK_SET) == 0, "seek through dup");
         check(read(copy, g_readback, sizeof(g_readback)) == sizeof(g_readback),
               "read payload through dup");
         check(memcmp(g_data, g_readback, sizeof(g_data)) == 0,
               "payload preserved");
         check(close(copy) == 0, "close dup");
       }
   
   #ifdef CONFIG_FS_LITTLEFS_ATTR_UPDATE
     check(fchmod(fd, 0604) == 0, "fchmod");
     mode = 0604;
     check_fd(fd, sizeof(g_data), mode);
   #endif
     check(rename("/lfsstat/dir/file", "/lfsstat/dir/renamed") == 0, "rename");
     check_fd(fd, sizeof(g_data), mode);
     check(close(fd) == 0, "close written file");
     for (i = 0; i < sizeof(g_flags) / sizeof(g_flags[0]); i++)
       {
         fd = open("/lfsstat/dir/renamed", g_flags[i]);
         check(fd >= 0, "reopen different access mode");
         if (fd >= 0)
           {
             check_fd(fd, sizeof(g_data), mode);
             check(close(fd) == 0, "close reopened file");
           }
       }
   
     memset(&filep, 0, sizeof(filep));
     ret = file_open(&filep, "/lfsstat/dir/renamed", O_RDONLY);
     check(ret == 0, "internal file_open");
     if (ret == 0)
       {
         ret = file_fstat(&filep, &st);
         check(ret == 0, "internal file_fstat");
         check(ret == 0 && S_ISREG(st.st_mode), "internal regular file type");
         check(file_close(&filep) == 0, "internal close");
       }
   
     check(umount("/lfsstat") == 0, "unmount for persistence");
     ret = mount("/dev/lfsstatmtd", "/lfsstat", "littlefs", 0, NULL);
     check(ret == 0, "remount without formatting");
     if (ret < 0)
       {
         goto unregister;
       }
   
     check_path("/lfsstat/dir/renamed", sizeof(g_data));
     fd = open("/lfsstat/dir/renamed", O_RDWR);
     check(fd >= 0, "open after remount");
     if (fd >= 0)
       {
         check_fd(fd, sizeof(g_data), mode);
         check(ftruncate(fd, 17) == 0, "truncate");
         check_fd(fd, 17, mode);
         check(fsync(fd) == 0, "sync truncation");
         check_path("/lfsstat/dir/renamed", 17);
         check(unlink("/lfsstat/dir/renamed") == 0, "unlink open file");
         check_fd(fd, 17, 0777);
         check(close(fd) == 0, "close unlinked file");
       }
   
     errno = 0;
     check(fstat(-1, &st) == -1 && errno == EBADF, "invalid fd error");
     check(rmdir("/lfsstat/dir") == 0, "remove directory");
   unmount:
     check(umount("/lfsstat") == 0, "final unmount");
   unregister:
     check(unregister_mtddriver("/dev/lfsstatmtd") == 0, "MTD unregister");
   free_mtd:
     rammtd_uninitialize(mtd);
   done:
     umask(saved_umask);
     printf("LFSSTAT RESULT checks=%u failures=%u\n", g_checks, g_failures);
     return g_failures != 0;
   }
   ```
   
   </details>
   
   ## PR verification Self-Check
   
   - [x] This PR introduces only one functional change.
   - [x] I have updated all required description fields above.
   - [ ] My PR adheres to all Contributing Guidelines and Documentation 
requirements (hardware runtime evidence is not available).
   - [ ] My PR is still work in progress (not ready for review).
   - [x] My PR is ready for review; validation scope and existing failures are 
disclosed above.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

  • [PR] fs/littlefs: report the ... jingfei195887 (via GitHub) via commits

Reply via email to