fs/vfs: Add new internal OS interface nx_read(). nx_read() is functionally equivalent to read() except that it does not modify the errno variable and it is not a cancellation point. Changed all references to read() in the OS to nx_read().

This commit is contained in:
Gregory Nutt 2017-10-11 12:13:41 -06:00
parent 957831d2ba
commit 181875f3ba
18 changed files with 222 additions and 141 deletions

View File

@ -47,6 +47,7 @@
#include <debug.h>
#include <errno.h>
#include <nuttx/fs/fs.h>
#include <nuttx/binfmt/elf.h>
/****************************************************************************
@ -134,18 +135,16 @@ int elf_read(FAR struct elf_loadinfo_s *loadinfo, FAR uint8_t *buffer,
/* Read the file data at offset into the user buffer */
nbytes = read(loadinfo->filfd, buffer, readsize);
nbytes = nx_read(loadinfo->filfd, buffer, readsize);
if (nbytes < 0)
{
int errval = errno;
/* EINTR just means that we received a signal */
if (errval != EINTR)
if (nbytes != -EINTR)
{
berr("Read from offset %lu failed: %d\n",
(unsigned long)offset, errval);
return -errval;
(unsigned long)offset, (int)nbytes);
return nbytes;
}
}
else if (nbytes == 0)

View File

@ -48,6 +48,8 @@
#include <errno.h>
#include <arpa/inet.h>
#include <nuttx/fs/fs.h>
#include <nuttx/binfmt/nxflat.h>
/****************************************************************************
@ -106,7 +108,8 @@ static inline void nxflat_dumpreaddata(FAR char *buffer, int buflen)
*
****************************************************************************/
int nxflat_read(struct nxflat_loadinfo_s *loadinfo, char *buffer, int readsize, int offset)
int nxflat_read(struct nxflat_loadinfo_s *loadinfo, char *buffer,
int readsize, int offset)
{
ssize_t nbytes; /* Number of bytes read */
off_t rpos; /* Position returned by lseek */
@ -135,14 +138,14 @@ int nxflat_read(struct nxflat_loadinfo_s *loadinfo, char *buffer, int readsize,
/* Read the file data at offset into the user buffer */
nbytes = read(loadinfo->filfd, bufptr, bytesleft);
nbytes = nx_read(loadinfo->filfd, bufptr, bytesleft);
if (nbytes < 0)
{
int errval = errno;
if (errval != EINTR)
if (nbytes != -EINTR)
{
berr("Read from offset %d failed: %d\n", offset, errval);
return -errval;
berr("Read from offset %d failed: %d\n",
offset, (int)nbytes);
return nbytes;
}
}
else if (nbytes == 0)

View File

@ -49,6 +49,7 @@
#include <nuttx/kmalloc.h>
#include <nuttx/poff.h>
#include <nuttx/fd/fs.h>
#include <nuttx/drivers/ramdisk.h>
#include <nuttx/binfmt/binfmt.h>
#include <nuttx/binfmt/pcode.h>
@ -404,20 +405,17 @@ static int pcode_load(struct binary_s *binp)
{
/* Read the next GULP */
nread = read(fd, ptr, remaining);
nread = nx_read(fd, ptr, remaining);
if (nread < 0)
{
/* If errno is EINTR, then this is not an error; the read() was
* simply interrupted by a signal.
/* If the failure is EINTR, then this is not an error; the
* nx_read() was simply interrupted by a signal.
*/
int errval = get_errno();
DEBUGASSERT(errval > 0);
if (errval != EINTR)
if (nread != -EINTR)
{
berr("ERROR: read failed: %d\n", errval);
ret = -errval;
berr("ERROR: read failed: %d\n", (int)nread);
ret = nread;
goto errout_with_fd;
}

View File

@ -1,7 +1,7 @@
/****************************************************************************
* configs/ea3131/src/lpc31_fillpage.c
*
* Copyright (C) 2010, 2012-2013 Gregory Nutt. All rights reserved.
* Copyright (C) 2010, 2012-2013, 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -52,6 +52,7 @@
# include <stdbool.h>
# include <unistd.h>
# include <fcntl.h>
# include <nuttx/fs/fs.h>
# ifdef CONFIG_EA3131_PAGING_SDSLOT
# include <stdio.h>
# include <sys/mount.h>
@ -439,7 +440,7 @@ int up_fillpage(FAR struct tcb_s *tcb, FAR void *vpage)
/* And read the page data from that offset */
nbytes = read(g_pgsrc.fd, vpage, PAGESIZE);
nbytes = nx_read(g_pgsrc.fd, vpage, PAGESIZE);
DEBUGASSERT(nbytes == PAGESIZE);
return OK;

View File

@ -1,7 +1,7 @@
/****************************************************************************
* configs/ea3152/src/lpc31_fillpage.c
*
* Copyright (C) 2011, 2013 Gregory Nutt. All rights reserved.
* Copyright (C) 2011, 2013, 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -52,6 +52,7 @@
# include <stdbool.h>
# include <unistd.h>
# include <fcntl.h>
# include <nuttx/fs/fs.h>
# ifdef CONFIG_EA3152_PAGING_SDSLOT
# include <stdio.h>
# include <sys/mount.h>
@ -439,7 +440,7 @@ int up_fillpage(FAR struct tcb_s *tcb, FAR void *vpage)
/* And read the page data from that offset */
nbytes = read(g_pgsrc.fd, vpage, PAGESIZE);
nbytes = nx_read(g_pgsrc.fd, vpage, PAGESIZE);
DEBUGASSERT(nbytes == PAGESIZE);
return OK;

View File

@ -47,9 +47,11 @@
#include <nuttx/random.h>
#include <nuttx/board.h>
#include <nuttx/fs/fs.h>
#include <nuttx/analog/adc.h>
#include <nuttx/analog/ioctl.h>
#include <arch/board/board.h>
#include "stm32l4_gpio.h"
#include "stm32l4_adc.h"
#include "nucleo-l452re.h"
@ -183,14 +185,13 @@ int stm32l4_adc_measure_voltages(uint32_t *vrefint, uint32_t *vbat, uint32_t *ve
goto out_close;
}
nbytes = read(fd, sample, sizeof(sample));
nbytes = nx_read(fd, sample, sizeof(sample));
if (nbytes < 0)
{
errval = errno;
ret = -errval;
if (errval != EINTR)
if (nbytes != -EINTR)
{
aerr("ERROR: read failed: %d\n", errval);
aerr("ERROR: nx_read() failed: %d\n", nbytes);
ret = (int)nbytes;
goto out_close;
}

View File

@ -259,11 +259,11 @@ static ssize_t loop_read(FAR struct inode *inode, FAR unsigned char *buffer,
do
{
nbytesread = read(dev->fd, buffer, nsectors * dev->sectsize);
if (nbytesread < 0 && get_errno() != EINTR)
nbytesread = nx_read(dev->fd, buffer, nsectors * dev->sectsize);
if (nbytesread < 0 && nbytesread != -EINTR)
{
_err("ERROR: Read failed: %d\n", get_errno());
return -get_errno();
_err("ERROR: Read failed: %d\n", nbytesread);
return (int)nbytesread;
}
}
while (nbytesread < 0);

View File

@ -162,7 +162,7 @@ static ssize_t filemtd_write(FAR struct file_dev_s *priv, size_t offset,
/* Read more data from the file */
lseek(priv->fd, seekpos, SEEK_SET);
buflen = read(priv->fd, buf, sizeof(buf));
buflen = nx_read(priv->fd, buf, sizeof(buf));
pout = (FAR uint8_t *) buf;
}
@ -231,9 +231,9 @@ static ssize_t filemtd_read(FAR struct file_dev_s *priv,
{
/* Set the starting location in the file */
lseek(priv->fd, priv->offset + offsetbytes, SEEK_SET);
(void)lseek(priv->fd, priv->offset + offsetbytes, SEEK_SET);
return read(priv->fd, buffer, nbytes);
return nx_read(priv->fd, buffer, nbytes);
}
/****************************************************************************
@ -325,7 +325,7 @@ static ssize_t filemtd_bread(FAR struct mtd_dev_s *dev, off_t startblock,
/* Then read the data from the file */
filemtd_read(priv, buf, offset, nbytes);
(void)filemtd_read(priv, buf, offset, nbytes);
return nblocks;
}
@ -387,7 +387,7 @@ static ssize_t filemtd_byteread(FAR struct mtd_dev_s *dev, off_t offset,
return 0;
}
filemtd_read(priv, buf, offset, nbytes);
(void)filemtd_read(priv, buf, offset, nbytes);
return nbytes;
}

View File

@ -55,6 +55,7 @@
#include "cc3000drv.h"
#include <nuttx/mqueue.h>
#include <nuttx/fs/fs.h>
#include <nuttx/wireless/cc3000.h>
#include <nuttx/wireless/cc3000/cc3000_common.h>
@ -144,7 +145,7 @@ long cc3000_write(uint8_t *pUserBuffer, uint16_t usLength)
long cc3000_read(uint8_t *pUserBuffer, uint16_t usLength)
{
DEBUGASSERT(spiconf.cc3000fd >= 0);
return read(spiconf.cc3000fd, pUserBuffer, usLength);
return nx_read(spiconf.cc3000fd, pUserBuffer, usLength);
}
/****************************************************************************

View File

@ -47,6 +47,7 @@
#include <errno.h>
#include <debug.h>
#include <nuttx/fs/fs.h>
#include <nuttx/kmalloc.h>
#include "inode/inode.h"
@ -174,29 +175,22 @@ FAR void *rammap(int fd, size_t length, off_t offset)
rdbuffer = map->addr;
while (length > 0)
{
nread = read(fd, rdbuffer, length);
nread = nx_read(fd, rdbuffer, length);
if (nread < 0)
{
/* Handle the special case where the read was interrupted by a
* signal.
*/
errcode = get_errno();
if (errcode != EINTR)
if (nread != -EINTR)
{
/* All other read errors are bad. errno is already set.
* (but maybe should be forced to EINVAL?). NOTE that if
* FS DEBUG is enabled, then the following ferr() macro will
* destroy the errno value.
*/
/* All other read errors are bad. */
ferr("ERROR: Read failed: offset=%d errno=%d\n",
(int)offset, errcode);
#ifdef CONFIG_DEBUG_FS
(int)offset, (int)nread);
errcode = (int)-ret;
goto errout_with_region;
#else
goto errout_with_errno;
#endif
}
}
@ -239,12 +233,6 @@ errout_with_region:
errout:
set_errno(errcode);
return MAP_FAILED;
#ifndef CONFIG_DEBUG_FS
errout_with_errno:
kumm_free(alloc);
return MAP_FAILED;
#endif
}
#endif /* CONFIG_FS_RAMMAP */

View File

@ -60,16 +60,22 @@
* Name: file_read
*
* Description:
* This is the internal implementation of read().
* file_read() is an interanl OS interface. It is functionally similar to
* the standard read() interface except:
*
* Parameters:
* file File structure instance
* buf User-provided to save the data
* nbytes The maximum size of the user-provided buffer
* - It does not modify the errno variable,
* - It is not a cancellation point,
* - It does not handle socket descriptors, and
* - It accepts a file structure instance instead of file descriptor.
*
* Return:
* Input Parameters:
* filep - File structure instance
* buf - User-provided to save the data
* nbytes - The maximum size of the user-provided buffer
*
* Returned Value:
* The positive non-zero number of bytes read on success, 0 on if an
* end-of-file condition, or -1 on failure with errno set appropriately.
* end-of-file condition, or a negated errno value on any failure.
*
****************************************************************************/
@ -110,30 +116,30 @@ ssize_t file_read(FAR struct file *filep, FAR void *buf, size_t nbytes)
}
/****************************************************************************
* Name: read
* Name: nx_read
*
* Description:
* The standard, POSIX read interface.
* nx_read() is an interanl OS interface. It is functionally similar to
* the standard read() interface except:
*
* Parameters:
* file File structure instance
* buf User-provided to save the data
* nbytes The maximum size of the user-provided buffer
* - It does not modify the errno variable, and
* - It is not a cancellation point.
*
* Return:
* Input Parameters:
* fd - File descriptor to read from
* buf - User-provided to save the data
* nbytes - The maximum size of the user-provided buffer
*
* Returned Value:
* The positive non-zero number of bytes read on success, 0 on if an
* end-of-file condition, or -1 on failure with errno set appropriately.
* end-of-file condition, or a negated errno value on any failure.
*
****************************************************************************/
ssize_t read(int fd, FAR void *buf, size_t nbytes)
ssize_t nx_read(int fd, FAR void *buf, size_t nbytes)
{
ssize_t ret;
/* read() is a cancellation point */
(void)enter_cancellation_point();
/* Did we get a valid file descriptor? */
#if CONFIG_NFILE_DESCRIPTORS > 0
@ -142,20 +148,14 @@ ssize_t read(int fd, FAR void *buf, size_t nbytes)
{
#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0
/* No.. If networking is enabled, read() is the same as recv() with
* the flags parameter set to zero. Note that recv() sets
* the errno variable.
* the flags parameter set to zero.
*/
ret = nx_recv(fd, buf, nbytes, 0);
if (ret < 0)
{
goto errout;
}
return nx_recv(fd, buf, nbytes, 0);
#else
/* No networking... it is a bad descriptor in any event */
ret = -EBADF;
goto errout;
return -EBADF;
#endif
}
@ -172,24 +172,54 @@ ssize_t read(int fd, FAR void *buf, size_t nbytes)
ret = (ssize_t)fs_getfilep(fd, &filep);
if (ret < 0)
{
goto errout;
return ret;
}
/* Then let file_read do all of the work. */
ret = file_read(filep, buf, nbytes);
return file_read(filep, buf, nbytes);
}
#else
/* I don't think we can get here */
return -ENOSYS;
#endif
}
/****************************************************************************
* Name: read
*
* Description:
* The standard, POSIX read interface.
*
* Input Parameters:
* fd - File descriptor to read from
* buf - User-provided to save the data
* nbytes - The maximum size of the user-provided buffer
*
* Returned Value:
* The positive non-zero number of bytes read on success, 0 on if an
* end-of-file condition, or -1 on failure with errno set appropriately.
*
****************************************************************************/
ssize_t read(int fd, FAR void *buf, size_t nbytes)
{
ssize_t ret;
/* read() is a cancellation point */
(void)enter_cancellation_point();
/* Let nx_read() do the real work */
ret = nx_read(fd, buf, nbytes);
if (ret < 0)
{
goto errout;
set_errno(-ret);
ret = ERROR;
}
}
#endif
leave_cancellation_point();
return ret;
errout:
set_errno((int)-ret);
leave_cancellation_point();
return (ssize_t)ERROR;
}

View File

@ -83,11 +83,13 @@
*/
#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
# define _NX_READ(f,b,s) nx_read(f,b,s)
# define _NX_WRITE(f,b,s) nx_write(f,b,s)
# define _NX_GETERRNO(r) (-(r))
# define _NX_SETERRNO(r) set_errno(-(r))
# define _NX_GETERRVAL(r) (r)
#else
# define _NX_READ(f,b,s) read(f,b,s)
# define _NX_WRITE(f,b,s) write(f,b,s)
# define _NX_GETERRNO(r) errno
# define _NX_SETERRNO(r)
@ -913,9 +915,22 @@ int fs_getfilep(int fd, FAR struct file **filep);
* Name: file_read
*
* Description:
* Equivalent to the standard read() function except that is accepts a
* struct file instance instead of a file descriptor. Currently used
* only by net_sendfile() and aio_read();
* file_read() is an interanl OS interface. It is functionally similar to
* the standard read() interface except:
*
* - It does not modify the errno variable,
* - It is not a cancellation point,
* - It does not handle socket descriptors, and
* - It accepts a file structure instance instead of file descriptor.
*
* Input Parameters:
* filep - File structure instance
* buf - User-provided to save the data
* nbytes - The maximum size of the user-provided buffer
*
* Returned Value:
* The positive non-zero number of bytes read on success, 0 on if an
* end-of-file condition, or a negated errno value on any failure.
*
****************************************************************************/
@ -923,6 +938,29 @@ int fs_getfilep(int fd, FAR struct file **filep);
ssize_t file_read(FAR struct file *filep, FAR void *buf, size_t nbytes);
#endif
/****************************************************************************
* Name: nx_read
*
* Description:
* nx_read() is an interanl OS interface. It is functionally similar to
* the standard read() interface except:
*
* - It does not modify the errno variable, and
* - It is not a cancellation point.
*
* Input Parameters:
* fd - File descriptor to read from
* buf - User-provided to save the data
* nbytes - The maximum size of the user-provided buffer
*
* Returned Value:
* The positive non-zero number of bytes read on success, 0 on if an
* end-of-file condition, or a negated errno value on any failure.
*
****************************************************************************/
ssize_t nx_read(int fd, FAR void *buf, size_t nbytes);
/****************************************************************************
* Name: file_write
*

View File

@ -152,7 +152,7 @@ ssize_t sendfile(int outfd, int infd, off_t *offset, size_t count)
{
/* Read a buffer of data from the infd */
nbytesread = read(infd, iobuffer, CONFIG_LIB_SENDFILE_BUFSIZE);
nbytesread = _NX_READ(infd, iobuffer, CONFIG_LIB_SENDFILE_BUFSIZE);
/* Check for end of file */
@ -174,14 +174,17 @@ ssize_t sendfile(int outfd, int infd, off_t *offset, size_t count)
else if (nbytesread < 0)
{
int errcode = _NX_GETERRNO(nbytesread);
/* EINTR is not an error (but will still stop the copy) */
#ifndef CONFIG_DISABLE_SIGNALS
if (errno != EINTR || ntransferred == 0)
if (errcode != EINTR || ntransferred == 0)
#endif
{
/* Read error. Break out and return the error condition. */
_NX_SETERRNO(nbytesread);
ntransferred = ERROR;
endxfr = true;
break;
@ -241,7 +244,9 @@ ssize_t sendfile(int outfd, int infd, off_t *offset, size_t count)
if (errcode != EINTR || ntransferred == 0)
#endif
{
/* Write error. Break out and return the error condition */
/* Write error. Break out and return the error
* condition.
*/
_NX_SETERRNO(nbyteswritten);
ntransferred = ERROR;

View File

@ -48,6 +48,7 @@
#include <errno.h>
#include <nuttx/module.h>
#include <nuttx/fs/fs.h>
#include <nuttx/lib/modlib.h>
/****************************************************************************
@ -128,10 +129,10 @@ int modlib_read(FAR struct mod_loadinfo_s *loadinfo, FAR uint8_t *buffer,
/* Read the file data at offset into the user buffer */
nbytes = read(loadinfo->filfd, buffer, readsize);
nbytes = _NX_READ(loadinfo->filfd, buffer, readsize);
if (nbytes < 0)
{
int errval = errno;
int errval = _NX_GETERRNO(nbytes);
/* EINTR just means that we received a signal */

View File

@ -46,6 +46,8 @@
#include <fcntl.h>
#include <errno.h>
#include <nuttx/fs/fs.h>
#include "libc.h"
/****************************************************************************
@ -154,13 +156,12 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream)
if (count > buffer_available)
{
bytes_read = read(stream->fs_fd, dest, count);
bytes_read = _NX_READ(stream->fs_fd, dest, count);
if (bytes_read < 0)
{
/* An error occurred on the read. The error code is
* in the 'errno' variable.
*/
/* An error occurred on the read. */
_NX_SETERRNO(bytes_read);
goto errout_with_errno;
}
else if (bytes_read == 0)
@ -202,7 +203,8 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream)
* into the buffer.
*/
bytes_read = read(stream->fs_fd, stream->fs_bufread,
bytes_read = _NX_READ(stream->fs_fd,
stream->fs_bufread,
buffer_available);
if (bytes_read < 0)
{
@ -210,6 +212,7 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream)
* in the 'errno' variable.
*/
_NX_SETERRNO(bytes_read);
goto errout_with_errno;
}
else if (bytes_read == 0)
@ -238,13 +241,14 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream)
while (count > 0)
{
bytes_read = read(stream->fs_fd, dest, count);
bytes_read = _NX_READ(stream->fs_fd, dest, count);
if (bytes_read < 0)
{
/* An error occurred on the read. The error code is
* in the 'errno' variable.
*/
_NX_SETERRNO(bytes_read);
goto errout_with_errno;
}
else if (bytes_read == 0)

View File

@ -1,7 +1,7 @@
/****************************************************************************
* libc/stdio/lib_rawsistream.c
*
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
* Copyright (C) 2014, 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -37,10 +37,14 @@
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <nuttx/fs/fs.h>
#include "libc.h"
/****************************************************************************
@ -61,7 +65,7 @@ static int rawsistream_getc(FAR struct lib_sistream_s *this)
/* Attempt to read one character */
nwritten = read(rthis->fd, &ch, 1);
nwritten = _NX_READ(rthis->fd, &ch, 1);
if (nwritten == 1)
{
this->nget++;

View File

@ -37,10 +37,14 @@
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <nuttx/fs/fs.h>
#include "libc.h"
/****************************************************************************
@ -61,7 +65,7 @@ static int rawinstream_getc(FAR struct lib_instream_s *this)
/* Attempt to read one character */
nwritten = read(rthis->fd, &ch, 1);
nwritten = _NX_READ(rthis->fd, &ch, 1);
if (nwritten == 1)
{
this->nget++;

View File

@ -58,6 +58,8 @@
#include <fcntl.h>
#include <errno.h>
#include <nuttx/fs/fs.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
@ -545,10 +547,10 @@ static int tzload(FAR const char *name,
sp->goback = sp->goahead = FALSE;
if (!name)
if (name == NULL)
{
name = TZDEFAULT;
if (!name)
if (name == NULL)
{
goto oops;
}
@ -563,7 +565,8 @@ static int tzload(FAR const char *name,
if (!doaccess)
{
p = TZDIR;
if (!p || sizeof lsp->fullname - 1 <= strlen(p) + strlen(name))
if (p == NULL ||
sizeof(lsp->fullname - 1) <= strlen(p) + strlen(name))
{
goto oops;
}
@ -593,7 +596,7 @@ static int tzload(FAR const char *name,
goto oops;
}
nread = read(fid, up->buf, sizeof up->buf);
nread = _NX_READ(fid, up->buf, sizeof up->buf);
if (close(fid) < 0 || nread <= 0)
{
goto oops;