2010-01-26 00:18:02 +01:00
|
|
|
/* This file contains file writing system call handlers.
|
|
|
|
*
|
|
|
|
* The entry points into this file are:
|
|
|
|
* do_write perform the WRITE file system call
|
|
|
|
* do_ftrunc perform the FTRUNC file system call
|
|
|
|
*
|
|
|
|
* Created:
|
|
|
|
* April 2009 (D.C. van Moolenbroek)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "inc.h"
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
static int write_file(struct inode *ino, u64_t *posp, size_t *countp,
|
2012-03-24 16:16:34 +01:00
|
|
|
cp_grant_id_t *grantp);
|
2010-01-26 00:18:02 +01:00
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* write_file *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
static int write_file(ino, posp, countp, grantp)
|
2010-01-26 00:18:02 +01:00
|
|
|
struct inode *ino;
|
|
|
|
u64_t *posp;
|
|
|
|
size_t *countp;
|
|
|
|
cp_grant_id_t *grantp;
|
|
|
|
{
|
|
|
|
/* Write data or zeroes to a file, depending on whether a valid pointer to
|
|
|
|
* a data grant was provided.
|
|
|
|
*/
|
|
|
|
u64_t pos;
|
|
|
|
size_t count, size;
|
|
|
|
vir_bytes off;
|
|
|
|
char *ptr;
|
|
|
|
int r, chunk;
|
|
|
|
|
|
|
|
assert(!IS_DIR(ino));
|
|
|
|
|
|
|
|
if ((r = get_handle(ino)) != OK)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
pos = *posp;
|
|
|
|
count = *countp;
|
|
|
|
|
|
|
|
assert(count > 0);
|
|
|
|
|
2012-04-09 18:08:26 +02:00
|
|
|
/* Use the buffer from below to eliminate extra copying. */
|
|
|
|
size = sffs_table->t_writebuf(&ptr);
|
2010-01-26 00:18:02 +01:00
|
|
|
off = 0;
|
|
|
|
|
|
|
|
while (count > 0) {
|
|
|
|
chunk = MIN(count, size);
|
|
|
|
|
|
|
|
if (grantp != NULL) {
|
|
|
|
r = sys_safecopyfrom(m_in.m_source, *grantp,
|
2012-06-16 03:46:15 +02:00
|
|
|
off, (vir_bytes) ptr, chunk);
|
2010-01-26 00:18:02 +01:00
|
|
|
|
|
|
|
if (r != OK)
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
/* Do this every time. We don't know what happens below. */
|
|
|
|
memset(ptr, 0, chunk);
|
|
|
|
}
|
|
|
|
|
2012-04-09 18:08:26 +02:00
|
|
|
if ((r = sffs_table->t_write(ino->i_file, ptr, chunk, pos)) <= 0)
|
2010-01-26 00:18:02 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
count -= r;
|
|
|
|
off += r;
|
2013-11-11 19:17:03 +01:00
|
|
|
pos += r;
|
2010-01-26 00:18:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
*posp = pos;
|
|
|
|
*countp = off;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* do_write *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int do_write()
|
2010-01-26 00:18:02 +01:00
|
|
|
{
|
|
|
|
/* Write data to a file.
|
|
|
|
*/
|
|
|
|
struct inode *ino;
|
|
|
|
u64_t pos;
|
|
|
|
size_t count;
|
|
|
|
cp_grant_id_t grant;
|
|
|
|
int r;
|
|
|
|
|
2012-04-09 18:08:26 +02:00
|
|
|
if (state.s_read_only)
|
2010-01-26 00:18:02 +01:00
|
|
|
return EROFS;
|
|
|
|
|
2010-05-10 15:26:00 +02:00
|
|
|
if ((ino = find_inode(m_in.REQ_INODE_NR)) == NULL)
|
2010-01-26 00:18:02 +01:00
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
if (IS_DIR(ino)) return EISDIR;
|
|
|
|
|
|
|
|
pos = make64(m_in.REQ_SEEK_POS_LO, m_in.REQ_SEEK_POS_HI);
|
|
|
|
count = m_in.REQ_NBYTES;
|
|
|
|
grant = m_in.REQ_GRANT;
|
|
|
|
|
2010-01-27 23:59:03 +01:00
|
|
|
if (count == 0) return EINVAL;
|
2010-01-26 00:18:02 +01:00
|
|
|
|
|
|
|
if ((r = write_file(ino, &pos, &count, &grant)) != OK)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
m_out.RES_SEEK_POS_HI = ex64hi(pos);
|
|
|
|
m_out.RES_SEEK_POS_LO = ex64lo(pos);
|
|
|
|
m_out.RES_NBYTES = count;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* do_ftrunc *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int do_ftrunc()
|
2010-01-26 00:18:02 +01:00
|
|
|
{
|
|
|
|
/* Change file size or create file holes.
|
|
|
|
*/
|
|
|
|
char path[PATH_MAX];
|
|
|
|
struct inode *ino;
|
2012-04-09 18:08:26 +02:00
|
|
|
struct sffs_attr attr;
|
2010-01-26 00:18:02 +01:00
|
|
|
u64_t start, end, delta;
|
|
|
|
size_t count;
|
|
|
|
int r;
|
|
|
|
|
2012-04-09 18:08:26 +02:00
|
|
|
if (state.s_read_only)
|
2010-01-26 00:18:02 +01:00
|
|
|
return EROFS;
|
|
|
|
|
2010-05-10 15:26:00 +02:00
|
|
|
if ((ino = find_inode(m_in.REQ_INODE_NR)) == NULL)
|
2010-01-26 00:18:02 +01:00
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
if (IS_DIR(ino)) return EISDIR;
|
|
|
|
|
|
|
|
start = make64(m_in.REQ_TRC_START_LO, m_in.REQ_TRC_START_HI);
|
|
|
|
end = make64(m_in.REQ_TRC_END_LO, m_in.REQ_TRC_END_HI);
|
|
|
|
|
2013-11-21 12:09:58 +01:00
|
|
|
if (end == 0) {
|
2010-01-26 00:18:02 +01:00
|
|
|
/* Truncate or expand the file. */
|
|
|
|
if ((r = verify_inode(ino, path, NULL)) != OK)
|
|
|
|
return r;
|
|
|
|
|
2012-04-09 18:08:26 +02:00
|
|
|
attr.a_mask = SFFS_ATTR_SIZE;
|
2010-01-26 00:18:02 +01:00
|
|
|
attr.a_size = start;
|
|
|
|
|
2012-04-09 18:08:26 +02:00
|
|
|
r = sffs_table->t_setattr(path, &attr);
|
2010-01-26 00:18:02 +01:00
|
|
|
} else {
|
|
|
|
/* Write zeroes to the file. We can't create holes. */
|
2013-11-21 12:09:58 +01:00
|
|
|
if (end <= start) return EINVAL;
|
2010-01-26 00:18:02 +01:00
|
|
|
|
2013-11-21 12:09:58 +01:00
|
|
|
delta = end - start;
|
2010-01-26 00:18:02 +01:00
|
|
|
|
|
|
|
if (ex64hi(delta) != 0) return EINVAL;
|
|
|
|
|
|
|
|
count = ex64lo(delta);
|
|
|
|
|
|
|
|
r = write_file(ino, &start, &count, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|