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

   ## Summary
   
   Renaming a non-empty pseudoFS directory copies its child list into a newly
   allocated directory inode, but the children's parent pointers still refer to
   the old inode. `FIOC_FILEPATH` on an open child then returns a broken path, 
and
   releasing the last reference to the old directory leaves dangling parents.
   
   Update each immediate child's parent after successfully removing the old 
name,
   under the existing inode tree write lock. Grandchildren retain their 
immediate
   parents, and child reference counts do not change.
   
   ## Impact
   
   - Functionality: YES, preserve the parent chain of a renamed pseudoFS 
subtree.
   - API, configuration, structure layout, build process, hardware and drivers:
     NO changes. No user adaptation or new permission policy is required.
   - Directory replacement, ancestor rejection and mounted filesystem rename
     semantics are unchanged. No dependency on another open PR.
   - Documentation: no new interface or configuration to document.
   
   ## Testing
   
   I confirm that changes are verified on the local setup and work as intended.
   
   Host: Linux x86_64, GCC 13.1.0. Serial builds with two CPUs.
   Base `3a1dbb62655edbd4fc556cb33df4325abc21ddc7`; head 
`cd417cbf800c4cd21dd56dd2ff08e6a742007148`.
   
   - `sim:nsh`, identical standalone test and configuration: **39 checks,
     5 failures before**; **636 checks, zero failures after**.
   - The baseline pins the old directory so it can safely demonstrate all three
     stale immediate-parent pointers and two incorrect `FIOC_FILEPATH` results
     without intentionally accessing freed memory. It stops at that failure and
     leaves the pins until simulator exit.
   - The fixed version moves the populated directory across two parents 20 
times.
     It checks three siblings (two devices and a subdirectory), a grandchild,
     sibling order, child reference counts, old-name removal, and both path 
lookup
     and reads on file descriptors opened before the first rename. After each
     move it releases the old directory, creates and removes a directory at the
     old path, then checks the open files again. Same-name rename, failed
     descendant rename, an empty-directory control and complete cleanup pass.
   - Attributes and links disabled: **636 checks, zero failures**.
     The script reports these two disabled options as warnings because it first
     enables the full configuration and then applies the explicit minimal
     overrides. The saved configuration confirms both are disabled as intended.
   - Small `sim:nsh` build with `DEFAULT_SMALL` and no local regression app:
     text/data/bss changes are **+48/+0/+0 bytes**.
     No speed or memory-saving claim is made.
   - Protected `qemu-armv7r:pnsh` and kernel `qemu-armv8a:knsh` builds compile
     the changed path. Make/CMake, OSTest, full-file/commit checkpatch and
     tested-HEAD verification pass.
   
   No real hardware, protected/kernel runtime, SMP or concurrent rename stress
   was tested. Functional builds have the existing `readline_common.c`
   maybe-uninitialized warning; the changed production file has no warnings.
   
   Testing logs before change:
   
   <details>
   <summary>Baseline</summary>
   
   ```text
   rev 3a1dbb62655 fcntl: give O_RSYNC its own flag bit build rc=0
   run rc=0 
log=/home/mi/test/nuttx_apache/fs-upstream/pr-rename-child-parent/baseline/run.log
   nsh> renameparentcheck
   FAIL direct child points to new directory errno=0
   FAIL direct child points to new directory errno=0
   FAIL direct child points to new directory errno=0
   PATH expected=/renameparent/right/long-renamed-directory/a actual=treea
   FAIL FIOC_FILEPATH follows new parent errno=0
   PATH expected=/renameparent/right/long-renamed-directory/middle/leaf 
actual=treemiddle/leaf
   FAIL FIOC_FILEPATH follows new parent errno=0
   RENAMEPARENT RESULT checks=39 failures=5
   nsh> echo RENAMEPARENT_DONE
   RENAMEPARENT_DONE
   nsh> poweroff
   ```
   
   </details>
   
   Testing logs after change:
   
   <details>
   <summary>branch</summary>
   
   ```text
   rev cd417cbf800 fs/vfs: update child parents after renaming a pseudoFS 
directory build rc=0
   run rc=0 
log=/home/mi/test/nuttx_apache/fs-upstream/pr-rename-child-parent/branch/run.log
   nsh> renameparentcheck
   RENAMEPARENT RESULT checks=636 failures=0
   nsh> echo RENAMEPARENT_DONE
   RENAMEPARENT_DONE
   nsh> poweroff
   ```
   
   </details>
   
   <details>
   <summary>branch-minimal</summary>
   
   ```text
   WARNING: CONFIG_FS_LINKS not enabled after olddefconfig
   WARNING: CONFIG_PSEUDOFS_ATTRIBUTES not enabled after olddefconfig
   rev cd417cbf800 fs/vfs: update child parents after renaming a pseudoFS 
directory build rc=0
   run rc=0 
log=/home/mi/test/nuttx_apache/fs-upstream/pr-rename-child-parent/branch-minimal/run.log
   nsh> renameparentcheck
   RENAMEPARENT RESULT checks=636 failures=0
   nsh> echo RENAMEPARENT_DONE
   RENAMEPARENT_DONE
   nsh> poweroff
   ```
   
   </details>
   
   <details>
   <summary>Final gate</summary>
   
   ```text
   target   : nuttx
   branch   : fs/rename-child-parent
   head     : cd417cbf800c4cd21dd56dd2ff08e6a742007148
   subject  : fs/vfs: update child parents after renaming a pseudoFS directory
   commits  : 1
   base     : 3a1dbb6265 (merge-base with upstream/master)
   behind   : 0 commits of upstream/master
   nuttx    : cd417cbf80
   apps     : a1b03f8926
   options  : CONFIG_PSEUDOFS_FILE CONFIG_PSEUDOFS_ATTRIBUTES CONFIG_FS_LINKS 
CONFIG_FS_NOTIFY CONFIG_FS_IOCTL_FILEPATH 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>size-baseline</summary>
   
   ```text
      text         data     bss     dec     hex filename
    550829         2800   12896  566525   8a4fd nuttx
   ```
   
   </details>
   
   <details>
   <summary>size-branch</summary>
   
   ```text
      text         data     bss     dec     hex filename
    550877         2800   12896  566573   8a52d nuttx
   ```
   
   </details>
   
   <details>
   <summary>protected</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.
   
   00013a84 T rename
   ```
   
   </details>
   
   <details>
   <summary>kernel</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.
   
   00000000402a222c T rename
   ```
   
   </details>
   
   Reproducer: place these files in `apps/examples/renameparentcheck`, configure
   `sim:nsh`, enable `EXAMPLES_RENAMEPARENTCHECK`, `FS_IOCTL_FILEPATH`, 
`FS_LINKS`,
   `PSEUDOFS_ATTRIBUTES` and `DEBUG_ASSERTIONS`, disable coverage, build and run
   `renameparentcheck`. The standalone test uses private inode interfaces to pin
   the old node and inspect ownership safely, so it is attached here rather than
   adding those private interfaces to the public apps test dependencies.
   
   <details>
   <summary>Regression app: renameparentcheck_main.c</summary>
   
   ```c
   /****************************************************************************
    * apps/examples/renameparentcheck/renameparentcheck_main.c
    *
    * SPDX-License-Identifier: Apache-2.0
    
****************************************************************************/
   
   /****************************************************************************
    * Included Files
    
****************************************************************************/
   
   #include <nuttx/config.h>
   
   #include <sys/ioctl.h>
   #include <sys/stat.h>
   #include <errno.h>
   #include <fcntl.h>
   #include <limits.h>
   #include <stdbool.h>
   #include <stdio.h>
   #include <string.h>
   #include <unistd.h>
   
   #include <nuttx/atomic.h>
   #include <nuttx/fs/fs.h>
   
   #include "inode/inode.h"
   
   /****************************************************************************
    * Pre-processor Definitions
    
****************************************************************************/
   
   #define ROOT "/renameparent"
   
   /****************************************************************************
    * 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 FAR struct inode *pin(FAR const char *path)
   {
     struct inode_search_s desc;
     FAR struct inode *node = NULL;
   
     if (check(inode_search_setup(&desc, path, true) == 0, "search setup"))
       {
         check(inode_find(&desc, &node) == 0, "pin inode");
         inode_search_release(&desc);
       }
   
     return node;
   }
   
   static ssize_t device_read(FAR struct file *filep, FAR char *buffer,
                              size_t length)
   {
     memset(buffer, 'R', length);
     return length;
   }
   
   static const struct file_operations g_operations =
   {
     .read = device_read
   };
   
   static void check_path(int fd, FAR const char *expected)
   {
     char path[PATH_MAX];
     char data;
   
     memset(path, 0, sizeof(path));
     check(ioctl(fd, FIOC_FILEPATH, (unsigned long)path) == 0,
           "FIOC_FILEPATH succeeds");
     if (strcmp(path, expected) != 0)
       {
         printf("PATH expected=%s actual=%s\n", expected, path);
       }
   
     check(strcmp(path, expected) == 0, "FIOC_FILEPATH follows new parent");
     check(read(fd, &data, 1) == 1 && data == 'R', "open child remains usable");
   }
   
   static void exercise(void)
   {
     FAR const char *suffixes[] =
     {
       "a", "middle", "z"
     };
   
     FAR struct inode *children[3];
     FAR struct inode *grandchild;
     FAR struct inode *old;
     FAR struct inode *current;
     struct stat st;
     char source[128];
     char target[128];
     char path[128];
     int refs[3];
     int fds[2];
     int i;
     int step;
   
     if (!check(mkdir(ROOT, 0700) == 0, "mkdir unique root") ||
         !check(mkdir(ROOT "/left", 0700) == 0, "mkdir left") ||
         !check(mkdir(ROOT "/right", 0700) == 0, "mkdir right") ||
         !check(mkdir(ROOT "/left/tree", 0700) == 0, "mkdir tree") ||
         !check(mkdir(ROOT "/left/tree/middle", 0700) == 0,
                "mkdir child directory"))
       {
         return;
       }
   
     if (!check(register_driver(ROOT "/left/tree/a", &g_operations,
                                 0600, NULL) == 0, "register first child") ||
         !check(register_driver(ROOT "/left/tree/z", &g_operations,
                                 0600, NULL) == 0, "register last child") ||
         !check(register_driver(ROOT "/left/tree/middle/leaf", &g_operations,
                                 0600, NULL) == 0, "register grandchild"))
       {
         return;
       }
   
     fds[0] = open(ROOT "/left/tree/a", O_RDONLY);
     fds[1] = open(ROOT "/left/tree/middle/leaf", O_RDONLY);
     if (!check(fds[0] >= 0 && fds[1] >= 0, "open children before rename"))
       {
         return;
       }
   
     for (i = 0; i < 3; i++)
       {
         snprintf(path, sizeof(path), ROOT "/left/tree/%s", suffixes[i]);
         children[i] = pin(path);
         if (children[i] == NULL)
           {
             return;
           }
   
         refs[i] = atomic_read(&children[i]->i_crefs);
       }
   
     grandchild = pin(ROOT "/left/tree/middle/leaf");
     if (grandchild == NULL)
       {
         return;
       }
   
     strlcpy(source, ROOT "/left/tree", sizeof(source));
     for (step = 0; step < 20; step++)
       {
         snprintf(target, sizeof(target), ROOT "/%s/%s",
                  step % 2 == 0 ? "right" : "left",
                  step % 2 == 0 ? "long-renamed-directory" : "tree");
         old = pin(source);
         if (old == NULL || !check(rename(source, target) == 0, "rename tree"))
           {
             return;
           }
   
         current = pin(target);
         if (current == NULL)
           {
             return;
           }
   
         inode_rlock();
         check(old != current, "rename allocates distinct directory node");
         check(old->i_child == NULL && old->i_parent == NULL,
               "old directory detached");
         check(current->i_child == children[0] &&
               children[0]->i_peer == children[1] &&
               children[1]->i_peer == children[2] &&
               children[2]->i_peer == NULL, "child order preserved");
         for (i = 0; i < 3; i++)
           {
             check(children[i]->i_parent == current,
                   "direct child points to new directory");
             check(atomic_read(&children[i]->i_crefs) == refs[i],
                   "child reference count unchanged");
           }
   
         check(grandchild->i_parent == children[1],
               "grandchild keeps its immediate parent");
         inode_runlock();
         snprintf(path, sizeof(path), "%s/a", target);
         check_path(fds[0], path);
         snprintf(path, sizeof(path), "%s/middle/leaf", target);
         check_path(fds[1], path);
         check(stat(source, &st) < 0 && errno == ENOENT, "old name gone");
         if (g_failures != 0)
           {
             /* Keep the baseline's stale parent alive until simulator exit. */
   
             return;
           }
   
         inode_release(old);
         inode_release(current);
         check(mkdir(source, 0700) == 0, "reuse old directory storage");
         check(rmdir(source) == 0, "remove churn directory");
         snprintf(path, sizeof(path), "%s/a", target);
         check_path(fds[0], path);
         snprintf(path, sizeof(path), "%s/middle/leaf", target);
         check_path(fds[1], path);
         strlcpy(source, target, sizeof(source));
       }
   
     check(rename(source, source) == 0, "same directory no-op");
     snprintf(target, sizeof(target), "%s/illegal", source);
     check(rename(source, target) == -1 && errno == EINVAL,
           "descendant rename rejected");
     current = pin(source);
     for (i = 0; i < 3; i++)
       {
         check(children[i]->i_parent == current,
               "failed rename preserves parent");
         inode_release(children[i]);
       }
   
     inode_release(current);
     inode_release(grandchild);
     check(close(fds[0]) == 0 && close(fds[1]) == 0, "close held files");
     check(unregister_driver(ROOT "/left/tree/a") == 0, "remove first child");
     check(unregister_driver(ROOT "/left/tree/z") == 0, "remove last child");
     check(unregister_driver(ROOT "/left/tree/middle/leaf") == 0,
           "remove grandchild");
     check(rmdir(ROOT "/left/tree/middle") == 0, "remove child directory");
     check(rmdir(ROOT "/left/tree") == 0, "remove tree");
     check(mkdir(ROOT "/left/empty", 0700) == 0, "mkdir empty control");
     check(rename(ROOT "/left/empty", ROOT "/right/empty") == 0,
           "rename empty control");
     check(rmdir(ROOT "/right/empty") == 0, "remove empty control");
     check(rmdir(ROOT "/left") == 0, "remove left");
     check(rmdir(ROOT "/right") == 0, "remove right");
     check(rmdir(ROOT) == 0, "remove test root");
   }
   
   /****************************************************************************
    * Public Functions
    
****************************************************************************/
   
   int main(int argc, FAR char *argv[])
   {
     exercise();
     printf("RENAMEPARENT RESULT checks=%u failures=%u\n",
            g_checks, g_failures);
     return g_failures != 0;
   }
   ```
   
   </details>
   
   <details>
   <summary>Regression app: Kconfig</summary>
   
   ```text
   config EXAMPLES_RENAMEPARENTCHECK
        bool "Local pseudoFS rename parent regression"
        default n
        depends on BUILD_FLAT && !DISABLE_PSEUDOFS_OPERATIONS
        select FS_IOCTL_FILEPATH
   ```
   
   </details>
   
   <details>
   <summary>Regression app: Makefile</summary>
   
   ```text
   include $(APPDIR)/Make.defs
   PROGNAME = renameparentcheck
   PRIORITY = SCHED_PRIORITY_DEFAULT
   STACKSIZE = 8192
   MODULE = $(CONFIG_EXAMPLES_RENAMEPARENTCHECK)
   MAINSRC = renameparentcheck_main.c
   CFLAGS += -I$(TOPDIR)/fs
   include $(APPDIR)/Application.mk
   ```
   
   </details>
   
   <details>
   <summary>Regression app: Make.defs</summary>
   
   ```text
   ifeq ($(CONFIG_EXAMPLES_RENAMEPARENTCHECK),y)
   CONFIGURED_APPS += $(APPDIR)/examples/renameparentcheck
   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: update child par... jingfei195887 (via GitHub) via commits

Reply via email to