]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
lib/ukfile: API: Change file offset to size_t
authorAndrei Tatar <andrei@unikraft.io>
Thu, 6 Feb 2025 12:20:44 +0000 (13:20 +0100)
committerUnikraft Bot <monkey@unikraft.io>
Tue, 11 Feb 2025 15:42:28 +0000 (15:42 +0000)
This change makes the ukfile API natively use size_t as type for
file offsets. The previous use of the signed off_t was unwieldy, as
negative file offsets had no meaning or use in the internal API.
Best to correct this mistake earlier than later.
This makes posix-fdio syscalls the only entry points with signed file
offset args, allowing validation to be isolated.

Signed-off-by: Andrei Tatar <andrei@unikraft.io>
Approved-by: Sergiu Moga <sergiu@unikraft.io>
Approved-by: Razvan Deaconescu <razvand@unikraft.io>
Reviewed-by: Sergiu Moga <sergiu@unikraft.io>
GitHub-Closes: #1580

lib/posix-eventfd/eventfd.c
lib/posix-fd/include/uk/posix-fd.h
lib/posix-fdio/fdio.c
lib/posix-pipe/pipe.c
lib/posix-socket/socket.c
lib/posix-timerfd/timerfd.c
lib/posix-tty/pseudo.c
lib/posix-tty/serial.c
lib/ukfile/file-nops.c
lib/ukfile/include/uk/file.h
lib/ukfile/include/uk/file/nops.h

index 64416da94e2d75139e474531d0da2a1cc81068f6..078590bf498d98bfe3f515551d882e603e061939 100644 (file)
@@ -36,7 +36,7 @@ struct evfd_alloc {
 
 static ssize_t evfd_read(const struct uk_file *f,
                         const struct iovec *iov, int iovcnt,
-                        off_t off, long flags __unused)
+                        size_t off, long flags __unused)
 {
        int semaphore;
        evfd_node n;
@@ -76,7 +76,7 @@ static ssize_t evfd_read(const struct uk_file *f,
 
 static ssize_t evfd_write(const struct uk_file *f,
                          const struct iovec *iov, int iovcnt,
-                         off_t off, long flags __unused)
+                         size_t off, long flags __unused)
 {
        uint64_t add;
        evfd_node n;
index 63436517b0145c402551b70a3ea91ea0d8911ac0..cec9de3787b6cfb2ca81601af6045e08eef29c50 100644 (file)
@@ -22,7 +22,7 @@ struct uk_ofile {
        const struct uk_file *file;
        unsigned int mode;
        __atomic refcnt;
-       off_t pos; /* Current file read/write offset position */
+       size_t pos; /* Current file read/write offset position */
        struct uk_mutex lock; /* Lock for modifying open file state */
 };
 
index ccdc08e19c0ae87062212f3b65413e18923410d7..91222156425ecc27f5035c3a027bab8370bb6624 100644 (file)
@@ -96,7 +96,7 @@ ssize_t uk_sys_readv(struct uk_ofile *of, const struct iovec *iov, int iovcnt)
 {
        ssize_t r;
        long flags;
-       off_t off;
+       size_t off;
        const struct uk_file *f;
        int seekable;
        int iolock;
@@ -152,7 +152,7 @@ ssize_t uk_sys_preadv2(struct uk_ofile *of, const struct iovec *iov, int iovcnt,
                       off_t offset, int flags)
 {
        ssize_t r;
-       off_t off;
+       size_t off;
        long xflags;
        const struct uk_file *f;
        unsigned int mode;
@@ -386,8 +386,10 @@ ssize_t uk_sys_pwritev2(struct uk_ofile *of, const struct iovec *iov,
        }
 
        if (use_pos) {
-               if (r >= 0)
+               if (r >= 0) {
+                       UK_ASSERT(off >= 0);
                        of->pos = off + r;
+               }
                _of_unlock(of);
        }
 
index a49612f5574d1226284ba85e4eb28691f7681ea2..3b06bc9704a5440502b8e661354cdfd7c2841645 100644 (file)
@@ -143,7 +143,7 @@ static ssize_t _iovsz(const struct iovec *iov, int iovcnt)
 
 static ssize_t pipe_read(const struct uk_file *f,
                         const struct iovec *iov, int iovcnt,
-                        off_t off, long flags __unused)
+                        size_t off, long flags __unused)
 {
        ssize_t toread;
        struct pipe_node *d;
@@ -231,7 +231,7 @@ static ssize_t pipe_read(const struct uk_file *f,
 
 static ssize_t pipe_write(const struct uk_file *f,
                          const struct iovec *iov, int iovcnt,
-                         off_t off, long flags)
+                         size_t off, long flags)
 {
        struct pipe_node *d;
        struct pipe_msg *tail;
index fa95d3a1b775dfcc379a21f406a4790f43f2dbb1..ccd43e0f03fe624e9c1f8f1ddbaea0166322eba6 100644 (file)
@@ -94,7 +94,7 @@ static struct uk_ofile *socketfd_get(int fd)
 static ssize_t
 socket_read(const struct uk_file *sock,
            const struct iovec *iov, int iovcnt,
-           off_t off, long flags __unused)
+           size_t off, long flags __unused)
 {
        ssize_t ret;
        struct posix_socket_driver *d;
@@ -126,7 +126,7 @@ socket_read(const struct uk_file *sock,
 static ssize_t
 socket_write(const struct uk_file *sock,
             const struct iovec *iov, int iovcnt,
-            off_t off, long flags __unused)
+            size_t off, long flags __unused)
 {
        ssize_t ret;
        struct posix_socket_driver *d;
index 202f38b2a0da6e2f3f35d60d44c8120f28a98db0..464a29dcb70dca9edd4623edd4679aaef8c3aa1d 100644 (file)
@@ -130,14 +130,14 @@ static void _timerfd_set(struct timerfd_node *d, const struct itimerspec *set)
 
 static ssize_t timerfd_read(const struct uk_file *f,
                            const struct iovec *iov, int iovcnt,
-                           off_t off, long flags __unused)
+                           size_t off, long flags __unused)
 {
        struct timerfd_node *d;
        __u64 v;
 
        if (unlikely(f->vol != TIMERFD_VOLID))
                return -EINVAL;
-       if (unlikely(off != 0))
+       if (unlikely(off))
                return -EINVAL;
        if (unlikely(!iovcnt || iov[0].iov_len < sizeof(__u64)))
                return -EINVAL;
index d5c201f121d3c8bf3638da22c26e07b36df38875..d351e3f8c9ae91b351ce5a5adc096ca7ab1ebd10 100644 (file)
@@ -23,7 +23,7 @@ static const char ZERO_VOLID[] = "zero_vol";
 
 static ssize_t null_read(const struct uk_file *f __maybe_unused,
                         const struct iovec *iov __unused, int iovcnt __unused,
-                        off_t off __unused, long flags __unused)
+                        size_t off __unused, long flags __unused)
 {
        UK_ASSERT(f->vol == NULL_VOLID);
        return 0;
@@ -31,7 +31,7 @@ static ssize_t null_read(const struct uk_file *f __maybe_unused,
 
 static ssize_t void_read(const struct uk_file *f __maybe_unused,
                         const struct iovec *iov __unused, int iovcnt __unused,
-                        off_t off __unused, long flags __unused)
+                        size_t off __unused, long flags __unused)
 {
        UK_ASSERT(f->vol == VOID_VOLID);
        return -EAGAIN;
@@ -39,7 +39,7 @@ static ssize_t void_read(const struct uk_file *f __maybe_unused,
 
 static ssize_t zero_read(const struct uk_file *f __maybe_unused,
                         const struct iovec *iov, int iovcnt,
-                        off_t off __unused, long flags __unused)
+                        size_t off __unused, long flags __unused)
 {
        ssize_t total = 0;
 
@@ -56,7 +56,7 @@ static ssize_t zero_read(const struct uk_file *f __maybe_unused,
 
 static ssize_t null_write(const struct uk_file *f __maybe_unused,
                          const struct iovec *iov, int iovcnt,
-                         off_t off __unused, long flags __unused)
+                         size_t off __unused, long flags __unused)
 {
        ssize_t total = 0;
 
index 3c0dfac1397e845f4fbda91e15cdcbd85ee231a9..00820ce4db8df39405828ecb15c8776687eb5b52 100644 (file)
@@ -89,13 +89,13 @@ static inline __ssz _console_out(const char *buf, __sz len)
 
 static ssize_t serial_read(const struct uk_file *f,
                           const struct iovec *iov, int iovcnt,
-                          off_t off, long flags __unused)
+                          size_t off, long flags __unused)
 {
        ssize_t total = 0;
 
        UK_ASSERT(f->vol == SERIAL_VOLID);
-       if (unlikely(off != 0))
-               return -EINVAL;
+       if (unlikely(off))
+               return -ESPIPE;
 
        if (!uk_file_poll_immediate(f, UKFD_POLLIN))
                return 0;
@@ -138,13 +138,13 @@ static ssize_t serial_read(const struct uk_file *f,
 
 static ssize_t serial_write(const struct uk_file *f __maybe_unused,
                            const struct iovec *iov, int iovcnt,
-                           off_t off, long flags __unused)
+                           size_t off, long flags __unused)
 {
        ssize_t total = 0;
 
        UK_ASSERT(f->vol == SERIAL_VOLID);
-       if (unlikely(off != 0))
-               return -EINVAL;
+       if (unlikely(off))
+               return -ESPIPE;
 
        for (int i = 0; i < iovcnt; i++) {
                char *buf = iov[i].iov_base;
index 44015c672cc6103fa83570645457dc5d07e66812..1109e7a3911447413e465e53ee5dbaf5fdc40a2b 100644 (file)
 
 ssize_t uk_file_nop_read(const struct uk_file *f __unused,
                         const struct iovec *iov __unused, int iovcnt __unused,
-                        off_t off __unused, long flags __unused)
+                        size_t off __unused, long flags __unused)
 {
        return -ENOSYS;
 }
 
 ssize_t uk_file_nop_write(const struct uk_file *f __unused,
                          const struct iovec *iov __unused, int iovcnt __unused,
-                         off_t off __unused, long flags __unused)
+                         size_t off __unused, long flags __unused)
 {
        return -ENOSYS;
 }
index 402a55ddfca171eda8b785a02b80fbdccc60a51a..1c7255dc88a358169388576c33be453995b74b6c 100644 (file)
@@ -37,7 +37,7 @@ struct uk_file;
 /* I/O */
 typedef ssize_t (*uk_file_io_func)(const struct uk_file *f,
                                   const struct iovec *iov, int iovcnt,
-                                  off_t off, long flags);
+                                  size_t off, long flags);
 
 /* Info (stat-like & chXXX-like) */
 typedef int (*uk_file_getstat_func)(const struct uk_file *f,
@@ -222,7 +222,7 @@ struct uk_file {
 static inline
 ssize_t uk_file_read(const struct uk_file *f,
                     const struct iovec *iov, int iovcnt,
-                    off_t off, long flags)
+                    size_t off, long flags)
 {
        return f->ops->read(f, iov, iovcnt, off, flags);
 }
@@ -230,7 +230,7 @@ ssize_t uk_file_read(const struct uk_file *f,
 static inline
 ssize_t uk_file_write(const struct uk_file *f,
                      const struct iovec *iov, int iovcnt,
-                     off_t off, long flags)
+                     size_t off, long flags)
 {
        return f->ops->write(f, iov, iovcnt, off, flags);
 }
index 668abfe035ffedaae5ffa5fd4d254f71ce4fa2ff..82644f5167d67040144cad4e928d8babf6ba565c 100644 (file)
@@ -15,11 +15,11 @@ extern const struct uk_file_ops uk_file_nops;
 
 ssize_t uk_file_nop_read(const struct uk_file *f,
                         const struct iovec *iov, int iovcnt,
-                        off_t off, long flags);
+                        size_t off, long flags);
 
 ssize_t uk_file_nop_write(const struct uk_file *f,
                          const struct iovec *iov, int iovcnt,
-                         off_t off, long flags);
+                         size_t off, long flags);
 
 int uk_file_nop_getstat(const struct uk_file *f,
                        unsigned int mask, struct uk_statx *arg);