Commit Graph

2430 Commits

Author SHA1 Message Date
Alan C. Assis
1f15756156 fs/fat: Ignore //... sequences in the file path
This fixes an issue reported by user harishn6 on github.

Signed-off-by: Alan C. Assis <acassis@gmail.com>
2025-10-23 08:44:12 -04:00
chao an
87f134cfaa sched/sleep: replace all Signal-based sleep implement to Scheduled sleep
Nuttx currently has 2 types of sleep interfaces:

1. Signal-scheduled sleep: nxsig_sleep() / nxsig_usleep() / nxsig_nanosleep()
Weaknesses:
a. Signal-dependent: The signal-scheduled sleep method is bound to the signal framework, while some driver sleep operations do not depend on signals.
b. Timespec conversion: Signal-scheduled sleep involves timespec conversion, which has a significant impact on performance.

2. Busy sleep: up_mdelay() / up_udelay()
Weaknesses:
a. Does not actively trigger scheduling, occupy the CPU loading.

3. New interfaces: Scheduled sleep: nxsched_sleep() / nxsched_usleep() / nxsched_msleep() / nxsched_ticksleep()
Strengths:
a. Does not depend on the signal framework.
b. Tick-based, without additional computational overhead.

Currently, the Nuttx driver framework extensively uses nxsig_* interfaces. However, the driver does not need to rely on signals or timespec conversion.
Therefore, a new set of APIs is added to reduce dependencies on other modules.

(This PR also aims to make signals optional, further reducing the code size of Nuttx.)

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-10-17 14:05:02 +08:00
chao an
10fd309452 sched/signal: Remove shadow definitions to reduce unnecessary API
Reduce unnecessary API definitions:

nxsig_waitinfo() -> nxsig_timedwait()

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-10-14 17:40:18 +08:00
raiden00pl
0dc88c1540 fs/mmap/Kconfig: fix reference to non-existent readme
fix reference to non-existent readme and point to the Documentation

Signed-off-by: raiden00pl <raiden00@railab.me>
2025-10-11 17:07:51 -04:00
Jukka Laitinen
d84bf53085 fs/driver: Add a generic function to find a driver pointer by filesystem path
Add a function to find any driver's registered pointer by the filesystem path.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-10-11 00:47:02 +08:00
p-szafonimateusz
20c9ff679c fs/mmap/fs_mmap.c: fix errno when fd is not valid
mmap() should return EBADF errno when fd is not valid.
We can just return error code from file_get().

Signed-off-by: p-szafonimateusz <p-szafonimateusz@xiaomi.com>
2025-09-30 16:16:02 -04:00
p-szafonimateusz
041c50e1dd fs/mmap/fs_mmap.c: add missing NULL pointer
add missing NULL pointer for msync to match `struct mm_map_entry_s` definition

Signed-off-by: p-szafonimateusz <p-szafonimateusz@xiaomi.com>
2025-09-30 23:28:41 +08:00
p-szafonimateusz
729f2f890b fs/mmap/fs_mmap.c: MAP_PRIVATE or MAP_SHARED must be specified
MAP_PRIVATE or MAP_SHARED must be specified in flags according to standard

Signed-off-by: p-szafonimateusz <p-szafonimateusz@xiaomi.com>
2025-09-30 23:28:32 +08:00
p-szafonimateusz
a4c9fdc447 fs/mmap/fs_msync.c: don't flush changes if MAP_PRIVATE
changes should not be flushed to the underlying file if memory mapping is
marked as MAP_PRIVATE.

Signed-off-by: p-szafonimateusz <p-szafonimateusz@xiaomi.com>
2025-09-30 23:27:52 +08:00
p-szafonimateusz
7439e49db8 fs/mmap/fs_munmap.c: return error if len is 0
munmap() shall return ERROR with errno = EINVAL if len is 0

Signed-off-by: p-szafonimateusz <p-szafonimateusz@xiaomi.com>
2025-09-30 23:27:45 +08:00
guoshengyuan1
5d7ac216f2 arch: optimize up_check_tcbstack for stack overflow detection
Many times, context switching does not occur when the thread stack
memory is almost exhausted, so we need to mofify up_check_tcbstack to
accurately detect it.

Co-authored-by: Chengdong Wang <wangchengdong@lixiang.com>
Signed-off-by: guoshengyuan1 <guoshengyuan1@xiaomi.com>
2025-09-27 12:56:11 +08:00
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
Some checks failed
Build Documentation / build-html (push) Has been cancelled
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