jingfei195887 opened a new pull request, #22422:
URL: https://github.com/apache/nuttx/pull/22422
## Summary
When two pseudoFS names refer to the same file through hard links, `rename()`
must return success and retain both names. Currently it compares the
directory
entry nodes, so it can remove a name and change the target's link/reference
counts even though the source and destination identify the same file.
Compare the underlying hard-link targets under the existing inode tree lock.
On a match, release the search buffer and lock and return without mutating
the
tree or emitting rename notifications. This also avoids false move events for
renaming a path onto itself. Terminal symbolic links are not dereferenced for
this comparison.
## Impact
- Functionality: YES, correct same-file pseudoFS rename to a no-op. For
example,
after `link("/source", "/alias")`, `rename("/source", "/alias")` succeeds
with both names intact.
- Reference counts, inode metadata and parent timestamps remain unchanged on
the no-op path. With FS_NOTIFY, no delete/move event is emitted for it.
- Public API, configuration, build process, structure layout,
hardware/drivers
and security/permission policy: NO changes. Existing permission checks
remain
before the identity check. User adaptation: none for POSIX rename callers.
- Mounted filesystem rename and distinct-file replacement: unchanged. This PR
does not change directory replacement semantics or transfer inode numbers.
- Documentation: no new interface or configuration option.
## Testing
I confirm that changes are verified on the local setup and work as intended.
Host: Linux x86_64, GCC 13.1.0. Builds ran serially with two CPUs.
Base `3a1dbb62655edbd4fc556cb33df4325abc21ddc7`; head
`ad3d82de22bb3bb4c0353c93a0132f6fd5a66c17`. Flat runtime target: `sim:nsh`.
The same standalone regression application and configuration were run on both
revisions. Each scenario boots a fresh simulator because a failing baseline
can
leave damaged hard-link ownership. It uses a character device with fixed data
and holds an open reference to the original inode while examining the result.
On success it checks all cleanup operations; a failing baseline leaves its
namespace untouched and the simulator exits before the next scenario.
Scenarios: alias-to-alias, original-to-alias, alias-to-original, identical
path,
and controls with distinct devices sharing the same operations/private value
and distinct symbolic links to the same target.
- Baseline: **28 failures in 157 checks**. Each affected
scenario stops after the first failing rename. Failures include lost names,
changed link/reference counts, changed metadata and false events. The
distinct
object controls pass.
- Branch: **2480 checks, zero failures**. Each no-op scenario
repeats 20 times (80 renames total); names, data, inode identity, counts,
timestamps and notification queues remain intact. Cleanup and controls
pass.
- PseudoFS attributes and FS_NOTIFY disabled: **2068
checks, zero failures**.
- FS_LINKS disabled: **1178 checks, zero failures**,
including same-path no-op notifications and ordinary replacement controls.
- Protected `qemu-armv7r:pnsh` and kernel `qemu-armv8a:knsh` build with
pseudoFS,
links and FS_NOTIFY enabled. Full-file/commit checkpatch, Make/CMake,
OSTest
and tested-HEAD verification pass.
- With the identical functional `sim:nsh` configuration and regression app,
text grows by 64 bytes (566693 to 566757); data remains 3088 bytes and bss
remains 12928 bytes. These figures include the local regression app.
No real hardware, protected/kernel runtime or concurrent rename stress was
tested. Baseline and functional builds show an unrelated `readline_common.c`
maybe-uninitialized warning; the changed production file has no warnings.
No memory-size or speed improvement is claimed.
Testing logs before change:
<details>
<summary>Baseline build and scenarios</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-hardlink/baseline-final/run.log
nsh> renamehardcheck 0
FAIL link count unchanged errno=0
FAIL target ctime unchanged errno=0
FAIL name remains accessible errno=2
FAIL link count unchanged errno=2
FAIL target ctime unchanged errno=2
FAIL inode references balanced errno=2
FAIL parent timestamps unchanged errno=2
FAIL no rename/delete event for no-op errno=2
RENAMEHARD RESULT mode=0 checks=34 failures=8
nsh> echo RENAMEHARD_DONE
RENAMEHARD_DONE
nsh> poweroff
nsh> renamehardcheck 1
FAIL name remains accessible errno=2
FAIL target inode unchanged errno=2
FAIL link count unchanged errno=2
FAIL target ctime unchanged errno=2
FAIL link count unchanged errno=2
FAIL mode unchanged errno=2
FAIL target ctime unchanged errno=2
FAIL open retained name errno=6
FAIL inode references balanced errno=6
FAIL parent timestamps unchanged errno=6
FAIL no rename/delete event for no-op errno=6
RENAMEHARD RESULT mode=1 checks=32 failures=11
nsh> echo RENAMEHARD_DONE
RENAMEHARD_DONE
nsh> poweroff
nsh> renamehardcheck 2
FAIL link count unchanged errno=0
FAIL target ctime unchanged errno=0
FAIL name remains accessible errno=2
FAIL link count unchanged errno=2
FAIL target ctime unchanged errno=2
FAIL inode references balanced errno=2
FAIL parent timestamps unchanged errno=2
FAIL no rename/delete event for no-op errno=2
RENAMEHARD RESULT mode=2 checks=34 failures=8
nsh> echo RENAMEHARD_DONE
RENAMEHARD_DONE
nsh> poweroff
nsh> renamehardcheck 3
FAIL no rename/delete event for no-op errno=0
RENAMEHARD RESULT mode=3 checks=41 failures=1
nsh> echo RENAMEHARD_DONE
RENAMEHARD_DONE
nsh> poweroff
nsh> renamehardcheck 4
RENAMEHARD RESULT mode=4 checks=16 failures=0
nsh> echo RENAMEHARD_DONE
RENAMEHARD_DONE
nsh> poweroff
```
</details>
Testing logs after change:
<details>
<summary>branch</summary>
```text
rev ad3d82de22b fs/vfs: preserve names when renaming hard links to the same
file build rc=0
run rc=0
log=/home/mi/test/nuttx_apache/fs-upstream/pr-rename-hardlink/branch/run.log
nsh> renamehardcheck 0
RENAMEHARD RESULT mode=0 checks=616 failures=0
nsh> echo RENAMEHARD_DONE
RENAMEHARD_DONE
nsh> poweroff
nsh> renamehardcheck 1
RENAMEHARD RESULT mode=1 checks=616 failures=0
nsh> echo RENAMEHARD_DONE
RENAMEHARD_DONE
nsh> poweroff
nsh> renamehardcheck 2
RENAMEHARD RESULT mode=2 checks=616 failures=0
nsh> echo RENAMEHARD_DONE
RENAMEHARD_DONE
nsh> poweroff
nsh> renamehardcheck 3
RENAMEHARD RESULT mode=3 checks=616 failures=0
nsh> echo RENAMEHARD_DONE
RENAMEHARD_DONE
nsh> poweroff
nsh> renamehardcheck 4
RENAMEHARD RESULT mode=4 checks=16 failures=0
nsh> echo RENAMEHARD_DONE
RENAMEHARD_DONE
nsh> poweroff
```
</details>
<details>
<summary>branch-minimal</summary>
```text
WARNING: CONFIG_FS_NOTIFY not enabled after olddefconfig
WARNING: CONFIG_PSEUDOFS_ATTRIBUTES not enabled after olddefconfig
rev ad3d82de22b fs/vfs: preserve names when renaming hard links to the same
file build rc=0
run rc=0
log=/home/mi/test/nuttx_apache/fs-upstream/pr-rename-hardlink/branch-minimal/run.log
nsh> renamehardcheck 0
RENAMEHARD RESULT mode=0 checks=513 failures=0
nsh> echo RENAMEHARD_DONE
RENAMEHARD_DONE
nsh> poweroff
nsh> renamehardcheck 1
RENAMEHARD RESULT mode=1 checks=513 failures=0
nsh> echo RENAMEHARD_DONE
RENAMEHARD_DONE
nsh> poweroff
nsh> renamehardcheck 2
RENAMEHARD RESULT mode=2 checks=513 failures=0
nsh> echo RENAMEHARD_DONE
RENAMEHARD_DONE
nsh> poweroff
nsh> renamehardcheck 3
RENAMEHARD RESULT mode=3 checks=513 failures=0
nsh> echo RENAMEHARD_DONE
RENAMEHARD_DONE
nsh> poweroff
nsh> renamehardcheck 4
RENAMEHARD RESULT mode=4 checks=16 failures=0
nsh> echo RENAMEHARD_DONE
RENAMEHARD_DONE
nsh> poweroff
```
</details>
<details>
<summary>branch-nolinks</summary>
```text
WARNING: CONFIG_FS_LINKS not enabled after olddefconfig
rev ad3d82de22b fs/vfs: preserve names when renaming hard links to the same
file build rc=0
run rc=0
log=/home/mi/test/nuttx_apache/fs-upstream/pr-rename-hardlink/branch-nolinks/run.log
nsh> renamehardcheck 0
RENAMEHARD RESULT mode=0 checks=292 failures=0
nsh> echo RENAMEHARD_DONE
RENAMEHARD_DONE
nsh> poweroff
nsh> renamehardcheck 1
RENAMEHARD RESULT mode=1 checks=292 failures=0
nsh> echo RENAMEHARD_DONE
RENAMEHARD_DONE
nsh> poweroff
nsh> renamehardcheck 2
RENAMEHARD RESULT mode=2 checks=292 failures=0
nsh> echo RENAMEHARD_DONE
RENAMEHARD_DONE
nsh> poweroff
nsh> renamehardcheck 3
RENAMEHARD RESULT mode=3 checks=292 failures=0
nsh> echo RENAMEHARD_DONE
RENAMEHARD_DONE
nsh> poweroff
nsh> renamehardcheck 4
RENAMEHARD RESULT mode=4 checks=10 failures=0
nsh> echo RENAMEHARD_DONE
RENAMEHARD_DONE
nsh> poweroff
```
</details>
<details>
<summary>Final gate</summary>
```text
target : nuttx
branch : fs/rename-hardlink-identity
head : ad3d82de22bb3bb4c0353c93a0132f6fd5a66c17
subject : fs/vfs: preserve names when renaming hard links to the same file
commits : 1
base : 3a1dbb6265 (merge-base with upstream/master)
behind : 0 commits of upstream/master
nuttx : ad3d82de22
apps : a1b03f8926
options : CONFIG_PSEUDOFS_FILE CONFIG_PSEUDOFS_ATTRIBUTES CONFIG_FS_LINKS
CONFIG_FS_NOTIFY 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>Baseline size</summary>
```text
text data bss dec hex filename
566693 3088 12928 582709 8e435 nuttx
```
</details>
<details>
<summary>Branch size</summary>
```text
text data bss dec hex filename
566757 3088 12928 582773 8e475 nuttx
```
</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.
00013a84 T rename
```
</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.
00000000402a222c T rename
```
</details>
To reproduce, place the following files under
`apps/examples/renamehardcheck`,
configure `sim:nsh`, enable `EXAMPLES_RENAMEHARDCHECK`, `FS_LINKS`,
`FS_NOTIFY`,
`PSEUDOFS_ATTRIBUTES` and `DEBUG_ASSERTIONS`, and run `renamehardcheck N` for
N=0..4, rebooting the simulator after each scenario. Disable coverage for
these
functional runs. The test app is local validation code and is not installed
by
this production patch.
<details>
<summary>Regression app: renamehardcheck_main.c</summary>
```c
/****************************************************************************
* apps/examples/renamehardcheck/renamehardcheck_main.c
*
* SPDX-License-Identifier: Apache-2.0
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <nuttx/atomic.h>
#include <nuttx/fs/fs.h>
#ifdef CONFIG_FS_NOTIFY
# include <sys/inotify.h>
#endif
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define ROOT "/renamehard"
#define TARGET ROOT "/target"
#define ALIAS1 ROOT "/alias1"
#define ALIAS2 ROOT "/alias2"
/****************************************************************************
* 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 ssize_t device_read(FAR struct file *filep, FAR char *buffer,
size_t buflen)
{
memset(buffer, (int)(uintptr_t)filep->f_inode->i_private, buflen);
return buflen;
}
static const struct file_operations g_operations =
{
.read = device_read
};
static int inode_refs(int fd)
{
FAR struct file *filep;
int refs = -1;
if (check(file_get(fd, &filep) == 0, "file_get"))
{
refs = atomic_read(&filep->f_inode->i_crefs);
file_put(filep);
}
return refs;
}
static void check_name(FAR const char *path, FAR const struct stat *before)
{
struct stat after;
char data;
int fd;
if (check(stat(path, &after) == 0, "name remains accessible"))
{
check(after.st_ino == before->st_ino, "target inode unchanged");
check(after.st_nlink == before->st_nlink, "link count unchanged");
check(after.st_mode == before->st_mode, "mode unchanged");
#ifdef CONFIG_PSEUDOFS_ATTRIBUTES
check(after.st_ctim.tv_sec == before->st_ctim.tv_sec &&
after.st_ctim.tv_nsec == before->st_ctim.tv_nsec,
"target ctime unchanged");
#endif
fd = open(path, O_RDONLY);
if (check(fd >= 0, "open retained name"))
{
check(read(fd, &data, 1) == 1 && data == 'A', "retained data");
check(close(fd) == 0, "close name");
}
}
}
static void distinct_names(void)
{
struct stat st;
char buffer[64];
int fd;
check(mkdir(ROOT, 0700) == 0, "create control parent");
check(register_driver(TARGET, &g_operations, 0600,
(FAR void *)(uintptr_t)'A') == 0, "control source");
check(register_driver(ALIAS1, &g_operations, 0600,
(FAR void *)(uintptr_t)'A') == 0, "control target");
check(rename(TARGET, ALIAS1) == 0,
"distinct nodes with same private data");
check(stat(TARGET, &st) == -1 && errno == ENOENT,
"distinct source removed");
fd = open(ALIAS1, O_RDONLY);
if (check(fd >= 0, "open distinct destination"))
{
check(read(fd, buffer, 1) == 1 && buffer[0] == 'A', "control data");
check(close(fd) == 0, "close distinct destination");
}
#ifdef CONFIG_FS_LINKS
check(symlink(ALIAS1, TARGET) == 0, "first symlink");
check(symlink(ALIAS1, ALIAS2) == 0, "second symlink");
check(rename(TARGET, ALIAS2) == 0, "distinct symlinks to same target");
check(lstat(TARGET, &st) == -1 && errno == ENOENT,
"symlink source removed");
check(readlink(ALIAS2, buffer, sizeof(buffer)) == strlen(ALIAS1) &&
memcmp(buffer, ALIAS1, strlen(ALIAS1)) == 0,
"symlink destination preserved");
check(unlink(ALIAS2) == 0, "remove control symlink");
#endif
check(unregister_driver(ALIAS1) == 0, "remove control device");
check(rmdir(ROOT) == 0, "remove control parent");
}
/****************************************************************************
* Public Functions
****************************************************************************/
int main(int argc, FAR char *argv[])
{
struct stat before;
struct stat parent;
struct stat after;
FAR const char *oldpath;
FAR const char *newpath;
int refs;
int pin;
int mode;
int i;
#ifdef CONFIG_FS_NOTIFY
char events[512];
int notify;
#endif
mode = argc > 1 ? atoi(argv[1]) : 0;
if (mode == 4)
{
distinct_names();
printf("RENAMEHARD RESULT mode=%d checks=%u failures=%u\n",
mode, g_checks, g_failures);
return g_failures != 0;
}
if (mode < 0 || mode > 3)
{
return 2;
}
check(mkdir(ROOT, 0700) == 0, "create parent");
check(register_driver(TARGET, &g_operations, 0600,
(FAR void *)(uintptr_t)'A') == 0, "register target");
pin = open(TARGET, O_RDONLY);
if (!check(pin >= 0, "pin original inode"))
{
return 2;
}
#ifdef CONFIG_FS_LINKS
check(link(TARGET, ALIAS1) == 0, "link first name");
check(link(ALIAS1, ALIAS2) == 0, "link through alias");
oldpath = mode == 1 || mode == 3 ? TARGET : ALIAS1;
newpath = mode == 0 ? ALIAS2 : mode == 1 ? ALIAS1 : TARGET;
#else
oldpath = TARGET;
newpath = TARGET;
#endif
check(stat(TARGET, &before) == 0, "stat original");
check(stat(ROOT, &parent) == 0, "stat parent");
refs = inode_refs(pin);
#ifdef CONFIG_FS_NOTIFY
notify = inotify_init1(IN_NONBLOCK);
check(notify >= 0, "inotify init");
check(inotify_add_watch(notify, ROOT, IN_MOVE | IN_DELETE) >= 0,
"watch rename/delete");
#endif
for (i = 0; i < 20; i++)
{
check(rename(oldpath, newpath) == 0, "rename same file succeeds");
check_name(TARGET, &before);
#ifdef CONFIG_FS_LINKS
check_name(ALIAS1, &before);
check_name(ALIAS2, &before);
#endif
check(inode_refs(pin) == refs, "inode references balanced");
check(stat(ROOT, &after) == 0, "stat parent after");
#ifdef CONFIG_PSEUDOFS_ATTRIBUTES
check(parent.st_mtim.tv_sec == after.st_mtim.tv_sec &&
parent.st_mtim.tv_nsec == after.st_mtim.tv_nsec &&
parent.st_ctim.tv_sec == after.st_ctim.tv_sec &&
parent.st_ctim.tv_nsec == after.st_ctim.tv_nsec,
"parent timestamps unchanged");
#endif
#ifdef CONFIG_FS_NOTIFY
check(read(notify, events, sizeof(events)) == -1 && errno == EAGAIN,
"no rename/delete event for no-op");
#endif
if (g_failures != 0)
{
break;
}
}
#ifdef CONFIG_FS_NOTIFY
check(close(notify) == 0, "close notifier");
#endif
/* A failing baseline may have transferred hard-link ownership incorrectly.
* Keep its damaged namespace untouched and reboot before the next case.
*/
if (g_failures == 0)
{
#ifdef CONFIG_FS_LINKS
check(unlink(ALIAS1) == 0, "unlink first alias");
check(unlink(ALIAS2) == 0, "unlink second alias");
#endif
check(close(pin) == 0, "close pin");
check(unregister_driver(TARGET) == 0, "unregister target");
check(rmdir(ROOT) == 0, "remove parent");
}
printf("RENAMEHARD RESULT mode=%d checks=%u failures=%u\n",
mode, g_checks, g_failures);
return g_failures != 0;
}
```
</details>
<details>
<summary>Regression app: Kconfig</summary>
```text
config EXAMPLES_RENAMEHARDCHECK
bool "Local pseudofs same-file rename regression"
default n
depends on BUILD_FLAT && !DISABLE_PSEUDOFS_OPERATIONS
```
</details>
<details>
<summary>Regression app: Makefile</summary>
```text
include $(APPDIR)/Make.defs
PROGNAME = renamehardcheck
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 8192
MODULE = $(CONFIG_EXAMPLES_RENAMEHARDCHECK)
MAINSRC = renamehardcheck_main.c
include $(APPDIR)/Application.mk
```
</details>
<details>
<summary>Regression app: Make.defs</summary>
```text
ifeq ($(CONFIG_EXAMPLES_RENAMEHARDCHECK),y)
CONFIGURED_APPS += $(APPDIR)/examples/renamehardcheck
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]