Commit graph

2419 commits

Author SHA1 Message Date
Ari Kimari
0ded247f44 fs/fat: Fix wrong alloc used in fat_zero_cluster()
Fat_zero_cluster() use fs_heap_malloc() for buffer that
is used to call fat_hwread().
Fat_hwread() must be called with IO buffer that have proper
alingment because it might use DMA.

Fix changes fs_heap_malloc() to fat_io_alloc() which uses
correct fat_dma_alloc() if CONFIG_FAT_DMAMEMORY is defined.

Signed-off-by: Ari Kimari <ari.kimari@tii.ae>
2025-08-29 19:18:32 +02:00
Zhe Weng
61c8240458 fs/lock: Allow driver lock
Sometimes we need to flock a driver node, which we can support easily.

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2025-08-12 20:17:06 +08:00
Theodore Karatapanis
95ca3e7ca5 drivers/misc/optee: Add mmap shm op + remove memfd
The previous approach with memfd has 3 problems:
1) The close operation on the memfd isn't tied with optee_shm_close,
   therefore close(fd) doesn't free the optee_shm struct allocated
   by the kernel.

2) The kernel unnecessarily maps the file descriptor to its memory,
   however only userspace should need to do that.

3) Since the kernel doesn't need to map the file descriptor we
   don't need to unmap it.

To use anonymous mapping, the prototype of map_anonymous() was
moved from fs/mmap/fs_anonmap.h to include/nuttx/fs/fs.h. Since
fs_anonmap.h didn't contain any other information it is deleted.

A type from fs/mmap/fs_rammap.h was moved to the public :
include/nuttx/fs/fs.h as well.

Signed-off-by: Theodore Karatapanis <tkaratapanis@census-labs.com>
2025-08-06 02:29:33 +08:00
Liu, Richard Jiayang
d2282ec26a fs/vfs/fs_rename: fix directory move operation.
* Recurrency is removed from filesystem directory rename.
* Fixes use after free in buffer that was used as output and argument.

Signed-off-by: Tomasz 'CeDeROM' CEDRO <tomek@cedro.info>
2025-07-31 01:53:59 +08:00
jingfei
c3e87dd3d1 drivers/fs: Control the behavior of FTL by passing oflags during the open process.
To save more space (equivalent to the size of one erase sector of
MTD device) and to achieve faster read and write speeds, a method
for direct writing was introduced at the FTL layer.
This can be accomplished simply by using the following oflags during
the open operation:

1. O_DIRECT. when this flag is passed in, ftl internally uses
   the direct write strategy and no read cache is used in ftl;
   otherwise, each write will be executed with the minimum
   granularity of flash erase sector size which means a
   "sector read back - erase sector - write sector" operation
   is performed by using a read cache buffer in heap.

2. O_SYNC. When this flag is passed in, we assume that the
   flash has been erased in advance and no erasure operation
   will be performed internally within ftl. O_SYNC will take
   effect only when both O_DIRECT and O_SYNC are passed in
   simultaneously.

3. For uniformity, we remapped the mount flag in mount.h and
   unified it with the open flag in fcntl.h. The repetitive
   parts of their definitions were reused, and the remaining
   part of the mount flag redefine to the unused bit of open
   flags.

Signed-off-by: jingfei <jingfei@xiaomi.com>
2025-07-16 14:11:41 +08:00
dongjiuzhu1
338132af8f fs/block_proxy: fix the issue of the refs count for filep being zeroed out by utilizing dup2
file_open will clear filep, include f_refs

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-07-15 19:46:24 +08:00
dongjiuzhu1
ae33447220 fs/vfs: fix bug about lost dup oflags in dup2
this issue is caused by commit: b8e30b54ec

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-07-15 19:46:24 +08:00
dongjiuzhu1
7ecfbcdaab fs/fs_files: restore fd before calling fdlist_install
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-06-26 09:31:14 -03:00
dongjiuzhu1
e85cdac2ed fs/vfs: clear filep when call file_open/file_mq_open to avoid random value
fix crash caused by stack random value

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-06-26 09:31:14 -03:00
Xiang Xiao
c9dc89142c fs: Move inotify.c from fs/notify/ to fs/vfs/
and merge fs/notify/notify.h into fs/vfs/vfs.h

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2025-06-23 18:17:10 -03:00
Xiang Xiao
1ea0ae8073 fs/vfs: Rename lock.h to vfs.h
vfs.h will contain other internal functions in the future

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2025-06-23 18:17:10 -03:00
wangjianyu3
2424b12857 fs/procfs: fix output format of fd info
Refered to PRINTF(3), the [v]snprintf returns the number of characters
printed (excluding the null byte used to end output to strings).

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2025-06-23 20:31:24 +08:00
Michal Lenc
01bfa1a500 fs/vfs/fs_close.c: avoid double free if CONFIG_FS_NOTIFY is set
Double free occurred in lib_put_pathbuffer if CONFIG_FS_NOTIFY option
was enabled. The second if statement has to be called only if the
close operation returned error. The bug was introduced in 14f5c48
and was causing misc/lib_tempbuffer.c:141 debug assertion.

Signed-off-by: Michal Lenc <michallenc@seznam.cz>
2025-06-16 20:23:53 +08:00
dongjiuzhu1
b0904e7c39 fs/shm: add O_CLOEXEC when allocate new struct fd to avoid lost flags.
fix bug about ltp_interfaces_shm_open_11_1

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-06-15 07:12:44 -03:00
dongjiuzhu1
5fd1ab8e0f fs/fcntl: using ioctl to implement FIOGCLEX/FIOCLEX/FIONCLEX
these command FIOGCLEX/FIOCLEX/FIONCLEX are related to struct fd,
so need to use ioctl to implement.

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-06-15 07:12:44 -03:00
SPRESENSE
2fa3b72b3c fs/smartfs: Fix a fatal bug about sector writing after seek
When writing to the next sector after the forward position has been written
by seek, the old sector buffer is used, which may corrupt the file system.
Therefore, the sector buffer must always be updated after a writing by seek.

Signed-off-by: SPRESENSE <41312067+SPRESENSE@users.noreply.github.com>
2025-06-13 20:42:36 +08:00
Ville Juven
b8e30b54ec fs/vfs: Separate file descriptors from file descriptions
This patch is a rework of the NuttX file descriptor implementation. The
goal is two-fold:

1. Improve POSIX compliance. The old implementation tied file description
to inode only, not the file struct. POSIX however dictates otherwise.
2. Fix a bug with descriptor duplication (dup2() and dup3()). There is
an existing race condition with this POSIX API that currently results
in a kernel side crash.

The crash occurs when a partially open / closed file descriptor is
duplicated. The reason for the crash is that even if the descriptor is
closed, the file might still be in use by the kernel (due to e.g. ongoing
write to file). The open file data is changed by file_dup3() and this
causes a crash in the device / drivers themselves as they lose access to
the inode and private data.

The fix is done by separating struct file into file and file descriptor
structs. The file struct can live on even if the descriptor is closed,
fixing the crash. This also fixes the POSIX issue, as two descriptors
can now point to the same file.

Signed-off-by: Ville Juven <ville.juven@unikie.com>
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-06-12 18:12:42 +08:00
dongjiuzhu1
a12d21e830 fs/file: unify prefix about file_xxx api, like file_open, file_ioctl
old:
fs_getfilep, fs_putfilep, fs_reffilep
new:
file_get, file_put, file_ref

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-06-12 18:12:42 +08:00
dongjiuzhu1
9ca5c1d9c6 fs/file: unify prefix f_ for member locked
update locked to f_locked

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-06-12 18:12:42 +08:00
dongjiuzhu1
70fc5c3e77 fs/dup: remove unnecessary backup about fdcheck_tag and fdsan_tag
1. The call to file_close_without_clear in file_dup3 does not clear
the tag information, so there is no need to back it up.
2. file_dup3 don't need to copy tag information, tag is only valid for fd.

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-06-12 18:12:42 +08:00
dongjiuzhu1
3bc3092e6a fs/fs: remove unnecessary FS_REFCOUNT config
Previously, this config was added to ensure that the size of the struct
file remained unchanged, thereby preventing the Flash memory of
resource-constrained MCUs from being unnecessarily increased.

However, we have now refactored the relationship between struct fd and struct file,
reducing their memory footprint in both Flash and RAM.
Consequently, this config can be removed.

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-06-12 18:12:42 +08:00
Ville Juven
1e787ea280 nuttx/fs: Reorganize the code for close, dup, et. al.
Currently the code is dumped into one massive file; fs_files. Move the
different logical parts into their own files.

Signed-off-by: Ville Juven <ville.juven@unikie.com>
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-06-12 18:12:42 +08:00
Ville Juven
3fe1d1a54d fs/pseudofs: Add missing bumping of inode refcount
When a new pseudofile is created, the inode reference count needs to
be bumped to protect the node.

Signed-off-by: Ville Juven <ville.juven@unikie.com>
2025-06-12 18:12:42 +08:00
wangjianyu3
5e94d4482b fs/vfs: check buffer count and pointer for iovec
There are iovecs provided by user such as readv().

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2025-06-10 09:58:25 +08:00
wangjianyu3
89df084b0e fs/vfs: check if all iov_base are accessible
Check if all `iov_base` are inside accessible address space.

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2025-06-10 09:58:25 +08:00
raiden00pl
3ca2357a0b fs: unify Private Types banners
unify Private Types banners according to NuttX coding standard

Signed-off-by: raiden00pl <raiden00@railab.me>
2025-05-28 10:17:15 +08:00
dongjiuzhu1
4f57ebce66 fs/poll: remove poll_fdsetup
Allow users to operate poll in the kernel using the file_poll
approach, as file is protected with reference counting,
making it more secure.

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-05-26 18:49:45 +08:00
dongjiuzhu1
77a0478095 fs/epoll: using filep with reference counting instead of file descriptors
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-05-26 18:49:45 +08:00
Alan Carvalho de Assis
f113d13cbf fs/inode: Fix inoderemove when removing an inode without parent
This modification ensures that inoderemove will error instead of
trying to remove an inode without parent.

This fix was implement by Richard Jiayang Liu.

Signed-off-by: Richard Jiayang Liu <rjliu3@illinois.edu>
2025-05-25 20:44:23 +08:00
Lars Kruse
e5b675d4dc refactor: fix spelling in private field names
Fix some misspelled field names.
These field names seem to be used only in private contexts.
Thus, the probability of external code accessing these fields is very
low.
In the rare case of external usage, compile time errors will easily
direct users to the new field name.
2025-05-24 09:44:22 -03:00
Lars Kruse
3ce85ca54e style: fix spelling in code comments and strings 2025-05-23 10:48:41 +08:00
zhangshoukui
f57a5a6802 fs/vfs/fs_dup.c: before file_allocate should restore minfd if define FDCHECK
When we use fcntl for dup, an fd is directly passed. If we have opened FDCHECK. we need to restore this file descriptor.

open FDCHECK and test this:

`
int main(int ac, char **av)
{
  int fd1= open("./1.txt", O_WRONLY | O_CREAT, 0666);
  if (fd1 < 0)
    {
      printf("open err\n");
      return fd1;
  }

  int fd2= open("./2.txt", O_WRONLY | O_CREAT, 0666);
  if (fd2 < 0)
    {
      printf("open err\n");
      close(fd1);
      return fd2;
    }

  //close(fd2);
  int fd3 = fcntl(fd1, F_DUPFD, fd2);
  printf("fd3 = %d\n", fd3);
  close(fd1);
  close(fd3);
  return 0;
}
`

Signed-off-by: zhangshoukui <zhangshoukui@xiaomi.com>
2025-05-15 16:51:40 -03:00
dongjiuzhu1
83967601aa fs/close: move inode to last to void race condition
race condition:

A Thread:                                    B Thread:
close
file_close
filep->inode = NULL
                         context switch
	               ------------------->  open to alloc same fd
                                             read from IO
filep->f_tag = 0       <-------------------
filep->f_san = 0       ------------------->  ioctl(fd, ...)
                                             fdcheck trigger assert

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-05-14 15:02:38 +08:00
Pip Cet
cfab00817a fs/procfs: Improve subdirectory recognition
This patch fixes https://github.com/apache/nuttx/issues/16237, where
cd'ing to a non-directory prefix of a procfs entry would succeed.

Signed-off-by: Pip Cet <pipcet@protonmail.com>
2025-05-02 09:28:06 -03:00
zhangshoukui
d3bb1eab0a fs/littlefs: Add read-only parameter
Test command:
./tools/configure.sh -l sim:tcpblaster
make -j
umount /mnt/lfs
mount -t littlefs -o ro /dev/rammtd /data

Signed-off-by: zhangshoukui <zhangshoukui@xiaomi.com>
2025-04-18 12:30:29 +08:00
zhangshoukui
60111786d6 fs/littlefs: Code comment correction
Signed-off-by: zhangshoukui <zhangshoukui@xiaomi.com>
2025-04-18 12:30:29 +08:00
YAMAMOTO Takashi
b59373b270 littlefs_open: sync the file only when O_TRUNC is specified
Because sync is a rather expensive operation.

While I'm here, improve the comment after the recent discussion
in https://github.com/apache/nuttx/pull/2913.

IMHO, we should not perform the sync even for O_TRUNC.
But I'm not going to propose the change for now.

Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
2025-04-10 14:38:18 +08:00
Beat Küng
ac2078a8bf fs/procfs: fix potential null pointer access in procfs_opendir
Some entries have the opendir function set to NULL, for example
g_mount_operations.

A null pointer dereference can be triggered by an
opendir("/proc/fs/blocks") for example.

Signed-off-by: Beat Küng <beat-kueng@gmx.net>
2025-04-09 11:50:35 -03:00
YAMAMOTO Takashi
90e9b2426d add a comment on littlefs_attr_s to explain what it is
i couldn't find any documenation about this attribute.
this comment is purely based on my reading of the code.

Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
2025-04-03 16:23:42 +08:00
Tiago Medicci Serrano
b1bab5c783 fs/vfs/fs_poll: Add noinstrument_function to poll_notify function
Add the noinstrument_function attribute to the poll_notify function
to avoid it being looped if -finstrument-functions is set to the
fs/vfs files.

Signed-off-by: Tiago Medicci Serrano <tiago.medicci@espressif.com>
2025-03-18 09:17:52 +08:00
dongjiuzhu1
cfd359141f fs/drivers: Avoid causing a busy loop in the program due to context switching induced by sem_post.
examples:
There are two threads involved: thread A with a priority of 100 and
thread B with a priority of 101. Here's how they interact:

When thread A releases a semaphore, thread B is scheduled to execute
some code and may reacquire the semaphore. If no other tasks are ready,
thread A will be scheduled to run.
This continuous process can lead to a busy loop.

Thread A:                                                   Thread B:

while (nxsem_get_value(&priv->wait, &semcount) >= 0 &&  <---
       semcount <= 0)                                      |  2)context switch
  {                                  1)contex switch       |
    nxsem_post(&priv->wait);         ------------->     run some code and nxsem_wait again
  }

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-03-17 10:44:26 +01:00
chenrun1
91d895d9a2 fs/virtio-9p:When virtio-9p is not supported, return -ENODEV
Summary:
  When the operating environment does not support virtio-9p and the virtio-9p server driver is not provided, -ENODEV should be returned for virtio-9p to avoid crash caused by continued execution.

Signed-off-by: chenrun1 <chenrun1@xiaomi.com>
2025-03-13 16:37:20 -03:00
hujun5
f22b93b337 sched/spin_lock: rename raw_spin_lock to spin_lock_notrace
Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-02-13 20:48:15 +08:00
Yanfeng Liu
225197b779 littlefs/cmake: fix build issue
This fixes issue on Ubuntu 22.04 with cmake v3.22.1:

```
/usr/bin/arm-none-eabi-gcc -D-DLFS_ASSERT=DEBUGASSERT -D-DLFS_CONFIG=/home/yf/Projects/Nuttx/nuttx/fs/littlefs/lfs_vfs.h -D-DLFS_DEBUG=finfo -D-DLFS_ERROR=ferr -D-DLFS_TRACE=finfo -D-DLFS_WARN=fwarn -DLFS_ATTR_MAX=1022 -DLFS_FILE_MAX=2147483647 -DLFS_NAME_MAX=32 -D__KERNEL__ -D__NuttX__
```

It happeed when building `qemu-armv7a:rpproxy_ivshmem`.

Signed-off-by: Yanfeng Liu <p-liuyanfeng9@xiaomi.com>
2025-02-11 23:22:07 +08:00
chao an
ace7f0c633 fs/spinlock: replace no sched_lock() version to improve the performance
after below change merge to kernel, spin_lock() will turn off preemption by default,
but this change is not applicable to all scenarios. The locations in the kernel that
use spin_lock() extensively only require short critical sections and do not trigger
scheduling, which leads to serious performance degradation of NuttX in AMP mode.

In this PR, I try to expose similar problems and hope that each subsystem will carefully check the code coverage

https://github.com/apache/nuttx/pull/14578
|commit b69111d16a
|Author: hujun5 <hujun5@xiaomi.com>
|Date:   Thu Jan 23 16:14:18 2025 +0800
|
|    spinlock: add sched_lock to spin_lock_irqsave
|
|    reason:
|    We aim to replace big locks with smaller ones. So we will use spin_lock_irqsave extensively to
|    replace enter_critical_section in the subsequent process. We imitate the implementation of Linux
|    by adding sched_lock to spin_lock_irqsave in order to address scenarios where sem_post occurs
|    within spin_lock_irqsave, which can lead to spinlock failures and deadlocks.
|
|    Signed-off-by: hujun5 <hujun5@xiaomi.com>

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-01-24 11:48:23 +08:00
Yanfeng Liu
4e8bcf1ba4 procfs/meminfo: align page fields to columns
meminfo before:
```
      total       used       free    maxused    maxfree  nused  nfree name
    3129344      10824    3118520      11184    3118104     25      2 Kmem
   13631488    1114112   12517376   12517376 Page
```
and after:

```
      total       used       free    maxused    maxfree  nused  nfree name
    3129344      10824    3118520      11184    3118104     25      2 Kmem
   13631488    1114112   12517376              12517376               Page
```
Signed-off-by: Yanfeng Liu <p-liuyanfeng9@xiaomi.com>
2025-01-22 20:29:07 +08:00
chao an
ff50bc191c fs/uio: do not overwrite the return value
Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-01-22 16:45:46 +08:00
chao an
2c9b287b2e fs/vfs: initialize uio only if lower implement readv/writev
to simple signle read/write logic, initialize uio only if lower implement readv/writev

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-01-22 16:45:46 +08:00
YAMAMOTO Takashi
ab9522fd23 file_readv_compat: add a comment to explain limitations 2025-01-22 14:29:35 +08:00
chenrun1
4d17c353dd v9fs:socket driver
Summary:
  1.Add new api for socket parsing header - v9fs_parse_size
  2.Add socket driver for 9pfs

Signed-off-by: chenrun1 <chenrun1@xiaomi.com>
2025-01-20 17:12:14 +08:00