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

   ## Summary
   
   Renaming a pseudoFS node currently changes its inode number: 
`pseudorename()` allocates a destination inode but does not copy the source 
`i_ino`. Preserve that field with the rest of the source state so 
`stat`/`lstat` and `readdir` continue to report the same file identity after a 
rename.
   
   ## Impact
   
   - Functionality: fixes the inode number reported after a pseudoFS rename, 
including repeated renames and replacement of a regular file.
   - API, structure layout, configuration, build process and hardware changes: 
NO.
   - User adaptation: NO. Existing directory-target and hardlink semantics are 
unchanged.
   - Documentation: no new feature or interface; existing filesystem 
documentation has no conflicting inode-number statement for pseudoFS rename.
   - Security: no permission, locking or lifetime changes.
   
   ## Testing
   
   I confirm that changes are verified on the local setup and work as intended.
   
   Base `695d40366dabf7f239c297fdbe57b2473c90cab3`; head 
`8c929674cf5df7089b28211122ac3b534e38d261`.
   Build host: Linux 6.8.0-138-generic x86_64; gcc (Ubuntu 
13.1.0-8ubuntu1~22.04) 13.1.0.
   
   - `sim:nsh`: a local regression application directly calls `rename`, 
`lstat`, `stat` and `readdir`. With `PSEUDOFS_ATTRIBUTES`, `FS_LINKS`, 
`PSEUDOFS_FILE` and assertions enabled, the identical baseline/branch 
configurations report **85 checks, 12 failures before → 85 checks, 0 failures 
after**. All baseline failures concern inode identity. Cases cover empty 
directories, cross-parent and repeated renames, empty regular files, 
replacement, symlinks, same-path no-op, missing source and rejected descendant 
paths.
   - With attributes and links disabled, the branch passes **71 checks, 0 
failures**.
   - Make/CMake `sim:nsh` builds and OSTest pass. `sim:nsh` is present in the 
CI CMake test list. Protected `qemu-armv7r:pnsh` and kernel `qemu-armv8a:knsh` 
compile with pseudoFS operations, attributes, links and pseudo files enabled.
   - Small `sim:nsh` images, with `DEFAULT_SMALL` and pseudoFS operations 
enabled, exclude the test application. The `.config` files are identical. GNU 
size deltas: text **+16**, data **+0**, bss **+0** bytes. Both images contain 
`rename`.
   
   Runtime validation is limited to the flat simulator. No real hardware, 
protected/kernel runtime or concurrent rename stress was tested. This patch 
does not change or claim to fix the other pseudoFS rename semantics.
   
   The regression is provided as a standalone local nuttx-apps application 
below; it is not installed by this OS patch. Put the four files in 
`apps/examples/renameidcheck`, configure `sim:nsh`, enable 
`EXAMPLES_RENAMEIDCHECK`, `PSEUDOFS_ATTRIBUTES`, `FS_LINKS` and 
`DEBUG_ASSERTIONS`, then run `make olddefconfig && make -j2`. Run `make` once 
more if the newly added application was not registered on the first build. Run 
`renameidcheck` from NSH. The same source fails on the base and passes on this 
patch.
   
   Testing logs before change:
   
   <details>
   <summary>Baseline build and runtime</summary>
   
   ```text
   rev 695d40366da serial/pl011: fix pl011_putc SMP race on IMSC causing RX 
input loss build rc=0
   run rc=0 
log=/home/mi/test/nuttx_apache/fs-upstream/pr-rename-inode-number/baseline/run.log
   nsh> renameidcheck
   IDENTITY directory cross-parent before=25 after=26 same=0
   FAIL inode number preserved errno=0
   FAIL readdir preserves inode number errno=2
   IDENTITY directory shorter-name before=26 after=27 same=0
   FAIL inode number preserved errno=2
   FAIL readdir preserves inode number errno=2
   FAIL identity across repeated renames errno=2
   IDENTITY regular file cross-parent before=28 after=29 same=0
   FAIL inode number preserved errno=22
   FAIL readdir preserves inode number errno=2
   IDENTITY regular file replacement before=29 after=31 same=0
   FAIL inode number preserved errno=2
   FAIL readdir preserves inode number errno=2
   FAIL replacement keeps source identity errno=2
   IDENTITY symlink cross-parent before=32 after=33 same=0
   FAIL inode number preserved errno=2
   FAIL readdir preserves inode number errno=2
   RENAMEID RESULT checks=85 failures=12
   nsh> echo RENAMEID_DONE
   RENAMEID_DONE
   nsh> poweroff
   ```
   
   </details>
   
   Testing logs after change:
   
   <details>
   <summary>branch build and runtime</summary>
   
   ```text
   rev 8c929674cf5 fs/vfs: preserve the inode number across pseudorename build 
rc=0
   run rc=0 
log=/home/mi/test/nuttx_apache/fs-upstream/pr-rename-inode-number/branch/run.log
   nsh> renameidcheck
   IDENTITY directory cross-parent before=25 after=25 same=1
   IDENTITY directory shorter-name before=25 after=25 same=1
   IDENTITY regular file cross-parent before=28 after=28 same=1
   IDENTITY regular file replacement before=28 after=28 same=1
   IDENTITY symlink cross-parent before=32 after=32 same=1
   RENAMEID RESULT checks=85 failures=0
   nsh> echo RENAMEID_DONE
   RENAMEID_DONE
   nsh> poweroff
   ```
   
   </details>
   
   <details>
   <summary>branch-minimal build and runtime</summary>
   
   ```text
   rev 8c929674cf5 fs/vfs: preserve the inode number across pseudorename build 
rc=0
   run rc=0 
log=/home/mi/test/nuttx_apache/fs-upstream/pr-rename-inode-number/branch-minimal/run.log
   nsh> renameidcheck
   IDENTITY directory cross-parent before=25 after=25 same=1
   IDENTITY directory shorter-name before=25 after=25 same=1
   IDENTITY regular file cross-parent before=28 after=28 same=1
   IDENTITY regular file replacement before=28 after=28 same=1
   RENAMEID RESULT checks=71 failures=0
   nsh> echo RENAMEID_DONE
   RENAMEID_DONE
   nsh> poweroff
   ```
   
   </details>
   
   <details>
   <summary>Final Make/CMake/OSTest gate</summary>
   
   ```text
   target   : nuttx
   branch   : fs/pseudorename-inode-number
   head     : 8c929674cf5df7089b28211122ac3b534e38d261
   subject  : fs/vfs: preserve the inode number across pseudorename
   commits  : 1
   base     : 695d40366d (merge-base with upstream/master)
   behind   : 0 commits of upstream/master
   nuttx    : 8c929674cf
   apps     : 13ec1c27bd
   options  : CONFIG_PSEUDOFS_FILE CONFIG_PSEUDOFS_ATTRIBUTES CONFIG_FS_LINKS 
CONFIG_DEBUG_ASSERTIONS CONFIG_DEBUG_FULLOPT CONFIG_COVERAGE_NONE
   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>protected build</summary>
   
   ```text
   qemu-armv7r:pnsh build PASS
   arm-none-eabi-gcc (Arm GNU Toolchain 13.2.rel1 (Build arm-13.7)) 13.2.1 
20231009
   Copyright (C) 2023 Free Software Foundation, Inc.
   This is free software; see the source for copying conditions.  There is NO
   warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   ```
   
   </details>
   
   <details>
   <summary>kernel build</summary>
   
   ```text
   qemu-armv8a:knsh build PASS
   aarch64-none-elf-gcc (GCC) 13.4.0
   Copyright (C) 2023 Free Software Foundation, Inc.
   This is free software; see the source for copying conditions.  There is NO
   warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   ```
   
   </details>
   
   <details>
   <summary>size-baseline</summary>
   
   ```text
      text         data     bss     dec     hex filename
    550813         2800   12896  566509   8a4ed nuttx
   ```
   
   </details>
   
   <details>
   <summary>size-branch</summary>
   
   ```text
      text         data     bss     dec     hex filename
    550829         2800   12896  566525   8a4fd nuttx
   ```
   
   </details>
   
   <details>
   <summary>Regression app: renameidcheck_main.c</summary>
   
   ```c
   /****************************************************************************
    * apps/examples/renameidcheck/renameidcheck_main.c
    *
    * SPDX-License-Identifier: Apache-2.0
    
****************************************************************************/
   
   /****************************************************************************
    * Included Files
    
****************************************************************************/
   
   #include <nuttx/config.h>
   #include <sys/stat.h>
   #include <dirent.h>
   #include <errno.h>
   #include <fcntl.h>
   #include <inttypes.h>
   #include <stdbool.h>
   #include <stdio.h>
   #include <string.h>
   #include <unistd.h>
   
   /****************************************************************************
    * Pre-processor Definitions
    
****************************************************************************/
   
   #define ROOT "/renameid"
   
   /****************************************************************************
    * Private Data
    
****************************************************************************/
   
   static unsigned int g_checks;
   static unsigned int g_failures;
   
   /****************************************************************************
    * Private Functions
    
****************************************************************************/
   
   static bool check(bool ok, FAR const char *label)
   {
     g_checks++;
     if (!ok)
       {
         g_failures++;
         printf("FAIL %s errno=%d\n", label, errno);
       }
   
     return ok;
   }
   
   static bool create_file(FAR const char *path)
   {
     int fd = open(path, O_CREAT | O_EXCL | O_RDWR, 0600);
   
     if (!check(fd >= 0, "create file"))
       {
         return false;
       }
   
     return check(close(fd) == 0, "close file");
   }
   
   static ino_t move(FAR const char *oldpath, FAR const char *newpath,
                     FAR const char *label)
   {
     struct stat before;
     struct stat after;
     struct stat missing;
     FAR struct dirent *entry;
     FAR DIR *dir;
     FAR const char *name = strrchr(newpath, '/') + 1;
     bool found = false;
   
     memset(&before, 0, sizeof(before));
     memset(&after, 0, sizeof(after));
     if (!check(lstat(oldpath, &before) == 0, "lstat before") ||
         !check(rename(oldpath, newpath) == 0, "rename") ||
         !check(lstat(newpath, &after) == 0, "lstat after"))
       {
         return 0;
       }
   
     printf("IDENTITY %s before=%ju after=%ju same=%d\n", label,
            (uintmax_t)before.st_ino, (uintmax_t)after.st_ino,
            before.st_ino == after.st_ino);
     check(before.st_ino == after.st_ino, "inode number preserved");
     check(before.st_mode == after.st_mode, "mode preserved");
     check(before.st_dev == after.st_dev, "device preserved");
     check(lstat(oldpath, &missing) == -1 && errno == ENOENT,
           "old path removed");
   
     dir = opendir(ROOT "/right");
     if (check(dir != NULL, "opendir destination parent"))
       {
         while ((entry = readdir(dir)) != NULL)
           {
             if (strcmp(entry->d_name, name) == 0)
               {
                 found = true;
                 check(entry->d_ino == before.st_ino,
                       "readdir preserves inode number");
                 break;
               }
           }
   
         check(found, "destination listed");
         check(closedir(dir) == 0, "closedir");
       }
   
     return before.st_ino;
   }
   
   /****************************************************************************
    * Public Functions
    
****************************************************************************/
   
   int main(int argc, FAR char *argv[])
   {
     struct stat saved;
     struct stat current;
     ino_t oldino;
     ino_t targetino;
   #ifdef CONFIG_FS_LINKS
     char target[64];
     ssize_t length;
   #endif
   
     check(mkdir(ROOT, 0700) == 0, "mkdir root");
     check(mkdir(ROOT "/left", 0700) == 0, "mkdir left");
     check(mkdir(ROOT "/right", 0700) == 0, "mkdir right");
   
     if (check(mkdir(ROOT "/left/d", 0710) == 0, "mkdir source"))
       {
         oldino = move(ROOT "/left/d", ROOT "/right/long-directory-name",
                       "directory cross-parent");
         move(ROOT "/right/long-directory-name", ROOT "/right/d",
              "directory shorter-name");
         check(stat(ROOT "/right/d", &saved) == 0, "stat directory");
         check(saved.st_ino == oldino, "identity across repeated renames");
         check(rename(ROOT "/right/d", ROOT "/right/d") == 0,
               "same path no-op");
         check(stat(ROOT "/right/d", &current) == 0, "stat after no-op");
         check(current.st_ino == saved.st_ino, "no-op identity");
         check(rename(ROOT "/right/d", ROOT "/right/d/child") == -1 &&
               errno == EINVAL, "descendant rejected");
         check(stat(ROOT "/right/d", &current) == 0, "stat after rejection");
         check(current.st_ino == saved.st_ino, "failed rename identity");
         check(rmdir(ROOT "/right/d") == 0, "rmdir renamed directory");
       }
   
     if (create_file(ROOT "/left/f"))
       {
         oldino = move(ROOT "/left/f", ROOT "/right/long-file-name",
                       "regular file cross-parent");
         if (create_file(ROOT "/right/replaced"))
           {
             check(stat(ROOT "/right/replaced", &saved) == 0,
                   "stat replaced file");
             targetino = saved.st_ino;
             check(targetino != oldino, "distinct file identities");
             move(ROOT "/right/long-file-name", ROOT "/right/replaced",
                  "regular file replacement");
             check(stat(ROOT "/right/replaced", &current) == 0,
                   "stat replacement");
             check(current.st_ino == oldino && current.st_ino != targetino,
                   "replacement keeps source identity");
             check(unlink(ROOT "/right/replaced") == 0,
                   "unlink replacement");
           }
       }
   
   #ifdef CONFIG_FS_LINKS
     if (check(symlink("/dev/null", ROOT "/left/link") == 0, "symlink"))
       {
         move(ROOT "/left/link", ROOT "/right/long-link-name",
              "symlink cross-parent");
         length = readlink(ROOT "/right/long-link-name", target,
                           sizeof(target));
         check(length == strlen("/dev/null") &&
               memcmp(target, "/dev/null", strlen("/dev/null")) == 0,
               "symlink target preserved");
         check(unlink(ROOT "/right/long-link-name") == 0, "unlink symlink");
       }
   #endif
   
     check(rename(ROOT "/missing", ROOT "/right/missing") == -1 &&
           errno == ENOENT, "missing source rejected");
     check(lstat(ROOT "/right/missing", &current) == -1 && errno == ENOENT,
           "failed rename creates no destination");
     check(rmdir(ROOT "/left") == 0, "remove left");
     check(rmdir(ROOT "/right") == 0, "remove right");
     check(rmdir(ROOT) == 0, "remove root");
     printf("RENAMEID RESULT checks=%u failures=%u\n", g_checks, g_failures);
     return g_failures != 0;
   }
   ```
   
   </details>
   
   <details>
   <summary>Regression app: Kconfig</summary>
   
   ```text
   config EXAMPLES_RENAMEIDCHECK
        bool "Local pseudofs rename identity regression"
        default n
        depends on BUILD_FLAT && !DISABLE_PSEUDOFS_OPERATIONS
        select PSEUDOFS_FILE
   ```
   
   </details>
   
   <details>
   <summary>Regression app: Makefile</summary>
   
   ```text
   include $(APPDIR)/Make.defs
   PROGNAME = renameidcheck
   PRIORITY = SCHED_PRIORITY_DEFAULT
   STACKSIZE = 8192
   MODULE = $(CONFIG_EXAMPLES_RENAMEIDCHECK)
   MAINSRC = renameidcheck_main.c
   include $(APPDIR)/Application.mk
   ```
   
   </details>
   
   <details>
   <summary>Regression app: Make.defs</summary>
   
   ```text
   ifeq ($(CONFIG_EXAMPLES_RENAMEIDCHECK),y)
   CONFIGURED_APPS += $(APPDIR)/examples/renameidcheck
   endif
   ```
   
   </details>
   
   ## PR verification Self-Check
   
   - [x] This PR introduces only one functional change.
   - [x] I have updated all required description fields above.
   - [x] My PR follows the contributing guidelines for scope, coding style and 
testing.
   - [ ] My PR is still work in progress (not ready for review).
   - [x] My PR is ready for review and can be safely merged into the codebase.
   


-- 
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/vfs: preserve the ino... jingfei195887 (via GitHub) via commits

Reply via email to