This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit a4ee9b4d3e80c1a83940413c1507e55e9fcbfea6
Author: Xiang Xiao <[email protected]>
AuthorDate: Mon Aug 31 16:34:28 2026 +0800

    libc/unistd: Add fallocate() and implement posix_fallocate() on it.
    
    NuttX only provided posix_fallocate(), while the fallocate64 mapping in
    fcntl.h dangled because plain fallocate() did not exist.  Add fallocate()
    as the single implementation (mode 0 extends via ftruncate; KEEP_SIZE is
    a validated no-op; other modes fail with EOPNOTSUPP) and turn
    posix_fallocate() into a thin wrapper that converts the return value to
    the POSIX error-number convention.  Also define the FALLOC_FL_* mode
    flags and register fallocate in libc.csv.
    
    Signed-off-by: Xiang Xiao <[email protected]>
    Signed-off-by: ganjing <[email protected]>
---
 include/fcntl.h                                    |  10 ++
 libs/libc/libc.csv                                 |   1 +
 libs/libc/unistd/CMakeLists.txt                    |   2 +-
 libs/libc/unistd/Make.defs                         |   2 +-
 .../{lib_posix_fallocate.c => lib_fallocate.c}     | 115 ++++++++++++++++-----
 5 files changed, 104 insertions(+), 26 deletions(-)

diff --git a/include/fcntl.h b/include/fcntl.h
index 8840ed1e4d6..2465ea6eb53 100644
--- a/include/fcntl.h
+++ b/include/fcntl.h
@@ -185,6 +185,15 @@
 #define F_SEAL_WRITE        0x0008 /* Prevent writes */
 #define F_SEAL_FUTURE_WRITE 0x0010 /* Prevent future writes while mapped */
 
+/* fallocate() mode flag */
+
+#define FALLOC_FL_KEEP_SIZE      0x01 /* Allocate without changing st_size */
+#define FALLOC_FL_PUNCH_HOLE     0x02 /* De-allocate a range (unsupported) */
+#define FALLOC_FL_COLLAPSE_RANGE 0x08 /* Remove a range (unsupported) */
+#define FALLOC_FL_ZERO_RANGE     0x10 /* Zero a range (unsupported) */
+#define FALLOC_FL_INSERT_RANGE   0x20 /* Insert a hole (unsupported) */
+#define FALLOC_FL_UNSHARE_RANGE  0x40 /* Unshare blocks (unsupported) */
+
 #if defined(CONFIG_FS_LARGEFILE)
 #  define F_GETLK64         F_GETLK
 #  define F_SETLK64         F_SETLK
@@ -234,6 +243,7 @@ int open(FAR const char *path, int oflag, ...);
 int openat(int dirfd, FAR const char *path, int oflag, ...);
 int fcntl(int fd, int cmd, ...);
 
+int fallocate(int fd, int mode, off_t offset, off_t len);
 int posix_fallocate(int fd, off_t offset, off_t len);
 
 #if defined(__cplusplus)
diff --git a/libs/libc/libc.csv b/libs/libc/libc.csv
index bc18ab6e94f..37b4d93c434 100644
--- a/libs/libc/libc.csv
+++ b/libs/libc/libc.csv
@@ -73,6 +73,7 @@
 "execvp","unistd.h","defined(CONFIG_LIBC_EXECFUNCS)","int","FAR const char 
*","FAR char *const[]|FAR char *const *"
 "execvpe","unistd.h","defined(CONFIG_LIBC_EXECFUNCS)","int","FAR const char 
*","FAR char *const[]|FAR char *const *","FAR char *const[]|FAR char *const *"
 "exit","stdlib.h","!defined(__KERNEL__)","noreturn","int"
+"fallocate","fcntl.h","","int","int","int","off_t","off_t"
 "fchdir","unistd.h","!defined(CONFIG_DISABLE_ENVIRON)","int","int"
 "fclose","stdio.h","defined(CONFIG_FILE_STREAM)","int","FAR FILE *"
 "fdatasync","unistd.h","","int","int"
diff --git a/libs/libc/unistd/CMakeLists.txt b/libs/libc/unistd/CMakeLists.txt
index 98f40fff338..c57798783b6 100644
--- a/libs/libc/unistd/CMakeLists.txt
+++ b/libs/libc/unistd/CMakeLists.txt
@@ -120,7 +120,7 @@ if(CONFIG_LIBC_EXECFUNCS)
   list(APPEND SRCS lib_execl.c lib_execle.c lib_execv.c lib_execvp.c)
 endif()
 
-list(APPEND SRCS lib_truncate.c lib_posix_fallocate.c)
+list(APPEND SRCS lib_truncate.c lib_fallocate.c)
 
 if(CONFIG_ARCH_HAVE_VFORK OR CONFIG_ARCH_HAVE_FORK)
   list(APPEND SRCS lib_fork.c)
diff --git a/libs/libc/unistd/Make.defs b/libs/libc/unistd/Make.defs
index 73c847e20ef..fddf9e0d962 100644
--- a/libs/libc/unistd/Make.defs
+++ b/libs/libc/unistd/Make.defs
@@ -73,7 +73,7 @@ ifeq ($(CONFIG_LIBC_EXECFUNCS),y)
 CSRCS += lib_execl.c lib_execle.c lib_execv.c lib_execvp.c
 endif
 
-CSRCS += lib_truncate.c lib_posix_fallocate.c
+CSRCS += lib_truncate.c lib_fallocate.c
 
 ifneq ($(CONFIG_ARCH_HAVE_VFORK)$(CONFIG_ARCH_HAVE_FORK),)
 CSRCS += lib_fork.c
diff --git a/libs/libc/unistd/lib_posix_fallocate.c 
b/libs/libc/unistd/lib_fallocate.c
similarity index 52%
rename from libs/libc/unistd/lib_posix_fallocate.c
rename to libs/libc/unistd/lib_fallocate.c
index 8c2d81385a2..3f328e77de9 100644
--- a/libs/libc/unistd/lib_posix_fallocate.c
+++ b/libs/libc/unistd/lib_fallocate.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * libs/libc/unistd/lib_posix_fallocate.c
+ * libs/libc/unistd/lib_fallocate.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -35,60 +35,127 @@
  ****************************************************************************/
 
 /****************************************************************************
- * Name: posix_fallocate
+ * Name: fallocate
  *
  * Description:
- *  The posix_fallocate() function shall ensure that any required storage for
- *  regular file data starting at offset and continuing for len bytes is
- *  allocated on the file system storage media. If posix_fallocate() returns
- *  successfully, subsequent writes to the specified file data shall not fail
- *  due to the lack of free space on the file system storage media.
+ *   fallocate() is a Linux-specific interface that manipulates the file
+ *   space for the file referred to by fd.  It is the single implementation
+ *   shared by posix_fallocate(), which is a thin wrapper around it.
  *
- *  If the offset+len is beyond the current file size, then posix_fallocate()
- *  shall adjust the file size to offset+len. Otherwise, the file size shall
- *  not be changed.
+ *   This is a truncate-based emulation:  with mode equal to zero it
+ *   ensures that the file is at least offset+len bytes long by extending
+ *   it via ftruncate() when necessary, which satisfies the default
+ *   allocation semantics of both interfaces.  It is implementation-defined
+ *   whether the underlying file system reserves media blocks eagerly.
  *
- *  It is implementation-defined whether a previous posix_fadvise() call
- *  influences allocation strategy.
+ *   FALLOC_FL_KEEP_SIZE is accepted as a no-op:  after validating the
+ *   arguments and the file descriptor, the call succeeds without changing
+ *   the file.  A truncate-based emulation cannot allocate space beyond
+ *   the current end of file without changing st_size, so the keep-size
+ *   semantics cannot be honored in general.
  *
- *  Space allocated via posix_fallocate() shall be freed by a successful call
- *  to creat() or open() that truncates the size of the file. Space allocated
- *  via posix_fallocate() may be freed by a successful call to ftruncate()
- *  that reduces the file size to a size smaller than offset+ len.
+ *   All other mode flags (FALLOC_FL_PUNCH_HOLE, FALLOC_FL_COLLAPSE_RANGE,
+ *   FALLOC_FL_ZERO_RANGE, FALLOC_FL_INSERT_RANGE, FALLOC_FL_UNSHARE_RANGE)
+ *   are not supported and fail with EOPNOTSUPP.
+ *
+ * Input Parameters:
+ *   fd     - File descriptor of the regular file to be manipulated.
+ *   mode   - Operation mode; zero or FALLOC_FL_KEEP_SIZE only.
+ *   offset - Start of the byte range to allocate.
+ *   len    - Length of the byte range to allocate.
  *
  * Returned Value:
- *   Upon successful completion, posix_fallocate() shall return zero;
- *   otherwise, an error number shall be returned to indicate the error.
+ *   Upon successful completion, fallocate() returns zero; otherwise -1
+ *   is returned and errno is set to indicate the error.
+ *
+ *   EINVAL      - offset or len was less than 0.
+ *   EFBIG       - offset+len exceeds the maximum file size.
+ *   EOPNOTSUPP  - mode contains flags other than FALLOC_FL_KEEP_SIZE.
+ *   EBADF       - fd is not a valid file descriptor (via fstat()).
+ *   Plus any error that fstat() or ftruncate() may report.
  *
  ****************************************************************************/
 
-int posix_fallocate(int fd, off_t offset, off_t len)
+int fallocate(int fd, int mode, off_t offset, off_t len)
 {
   struct stat st;
 
   if (offset < 0 || len < 0)
     {
-      return EINVAL;
+      set_errno(EINVAL);
+      return ERROR;
+    }
+
+  if (mode & ~FALLOC_FL_KEEP_SIZE)
+    {
+      set_errno(EOPNOTSUPP);
+      return ERROR;
     }
 
   len += offset;
   if (len < 0)
     {
-      return EFBIG;
+      set_errno(EFBIG);
+      return ERROR;
     }
 
   if (fstat(fd, &st) != 0)
     {
-      return get_errno();
+      return ERROR;
+    }
+
+  if (mode & FALLOC_FL_KEEP_SIZE)
+    {
+      return OK;
     }
 
   if (st.st_size < len)
     {
       if (ftruncate(fd, len) != 0)
         {
-          return get_errno();
+          return ERROR;
         }
     }
 
-  return 0;
+  return OK;
+}
+
+/****************************************************************************
+ * Name: posix_fallocate
+ *
+ * Description:
+ *  The posix_fallocate() function shall ensure that any required storage for
+ *  regular file data starting at offset and continuing for len bytes is
+ *  allocated on the file system storage media. If posix_fallocate() returns
+ *  successfully, subsequent writes to the specified file data shall not fail
+ *  due to the lack of free space on the file system storage media.
+ *
+ *  If the offset+len is beyond the current file size, then posix_fallocate()
+ *  shall adjust the file size to offset+len. Otherwise, the file size shall
+ *  not be changed.
+ *
+ *  It is implementation-defined whether a previous posix_fadvise() call
+ *  influences allocation strategy.
+ *
+ *  Space allocated via posix_fallocate() shall be freed by a successful call
+ *  to creat() or open() that truncates the size of the file. Space allocated
+ *  via posix_fallocate() may be freed by a successful call to ftruncate()
+ *  that reduces the file size to a size smaller than offset+ len.
+ *
+ * Input Parameters:
+ *   fd     - File descriptor of the regular file to be manipulated.
+ *   offset - Start of the byte range to allocate.
+ *   len    - Length of the byte range to allocate.
+ *
+ * Returned Value:
+ *   Upon successful completion, posix_fallocate() shall return zero;
+ *   otherwise, an error number shall be returned to indicate the error.
+ *
+ ****************************************************************************/
+
+int posix_fallocate(int fd, off_t offset, off_t len)
+{
+  int ret = fallocate(fd, 0, offset, len);
+
+  return ret < 0 ? get_errno() : 0;
 }

Reply via email to