minix/servers/pfs/read.c

91 lines
2.7 KiB
C
Raw Normal View History

2009-12-20 21:41:50 +01:00
#include "fs.h"
#include "buf.h"
#include <minix/com.h>
#include "inode.h"
/*===========================================================================*
* fs_readwrite *
*===========================================================================*/
PUBLIC int fs_readwrite(message *fs_m_in, message *fs_m_out)
2009-12-20 21:41:50 +01:00
{
int r, rw_flag;
struct buf *bp;
cp_grant_id_t gid;
off_t position, f_size;
unsigned int nrbytes, cum_io;
mode_t mode_word;
struct inode *rip;
ino_t inumb;
r = OK;
cum_io = 0;
inumb = (ino_t) fs_m_in->REQ_INODE_NR;
2009-12-20 21:41:50 +01:00
/* Find the inode referred */
if ((rip = find_inode(inumb)) == NULL) return(EINVAL);
2009-12-20 21:41:50 +01:00
mode_word = rip->i_mode & I_TYPE;
if (mode_word != I_NAMED_PIPE) return(EIO);
f_size = rip->i_size;
/* Get the values from the request message */
rw_flag = (fs_m_in->m_type == REQ_READ ? READING : WRITING);
gid = (cp_grant_id_t) fs_m_in->REQ_GRANT;
position = fs_m_in->REQ_SEEK_POS_LO;
nrbytes = (unsigned) fs_m_in->REQ_NBYTES;
2010-05-28 11:39:18 +02:00
/* We can't read beyond the max file position */
if (nrbytes > MAX_FILE_POS) return(EFBIG);
2009-12-20 21:41:50 +01:00
if (rw_flag == WRITING) {
/* Check in advance to see if file will grow too big. */
2010-05-28 11:39:18 +02:00
/* Casting nrbytes to signed is safe, because it's guaranteed not to
be beyond max signed value (i.e., MAX_FILE_POS). */
if (position > PIPE_BUF - (signed) nrbytes) return(EFBIG);
2009-12-20 21:41:50 +01:00
}
/* Mark inode in use */
if ((get_inode(rip->i_dev, rip->i_num)) == NULL) return(err_code);
if ((bp = get_block(rip->i_dev, rip->i_num)) == NULL) return(err_code);
2009-12-20 21:41:50 +01:00
if (rw_flag == READING) {
/* Copy a chunk from the block buffer to user space. */
r = sys_safecopyto(VFS_PROC_NR, gid, (vir_bytes) 0,
2010-05-28 11:39:18 +02:00
(vir_bytes) (bp->b_data+position), (size_t) nrbytes, D);
2009-12-20 21:41:50 +01:00
} else {
/* Copy a chunk from user space to the block buffer. */
r = sys_safecopyfrom(VFS_PROC_NR, gid, (vir_bytes) 0,
2010-05-28 11:39:18 +02:00
(vir_bytes) (bp->b_data+position), (size_t) nrbytes, D);
2009-12-20 21:41:50 +01:00
}
if (r == OK) {
2010-05-28 11:39:18 +02:00
position += (signed) nrbytes; /* Update position */
2009-12-20 21:41:50 +01:00
cum_io += nrbytes;
}
fs_m_out->RES_SEEK_POS_LO = position; /* It might change later and the VFS
2009-12-20 21:41:50 +01:00
has to know this value */
/* On write, update file size and access time. */
if (rw_flag == WRITING) {
if (position > f_size) rip->i_size = position;
} else {
if(position >= rip->i_size) {
/* All data in the pipe is read, so reset pipe pointers */
rip->i_size = 0; /* no data left */
position = 0; /* reset reader(s) */
}
}
bp->b_bytes = position;
if (rw_flag == READING) rip->i_update |= ATIME;
if (rw_flag == WRITING) rip->i_update |= CTIME | MTIME;
fs_m_out->RES_NBYTES = (size_t) cum_io;
2009-12-20 21:41:50 +01:00
put_inode(rip);
put_block(rip->i_dev, rip->i_num);
return(r);
}