2011-11-14 12:53:05 +01:00
|
|
|
/* Created (MFS based):
|
|
|
|
* February 2010 (Evgeniy Ivanov)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fs.h"
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
|
|
|
#include "puffs.h"
|
|
|
|
#include "puffs_priv.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define GETDENTS_BUFSIZ 4096
|
2012-03-25 20:25:53 +02:00
|
|
|
static char getdents_buf[GETDENTS_BUFSIZ];
|
2011-11-14 12:53:05 +01:00
|
|
|
|
2014-08-24 11:51:35 +02:00
|
|
|
#define RW_BUFSIZ (128 * 1024)
|
2012-03-25 20:25:53 +02:00
|
|
|
static char rw_buf[RW_BUFSIZ];
|
2011-11-14 12:53:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
2014-08-24 11:51:35 +02:00
|
|
|
* fs_read *
|
2011-11-14 12:53:05 +01:00
|
|
|
*===========================================================================*/
|
2014-08-24 11:51:35 +02:00
|
|
|
ssize_t fs_read(ino_t ino_nr, struct fsdriver_data *data, size_t bytes,
|
|
|
|
off_t pos, int call)
|
2011-11-14 12:53:05 +01:00
|
|
|
{
|
2014-08-24 11:51:35 +02:00
|
|
|
int r;
|
|
|
|
size_t bytes_left, bytes_done;
|
2011-11-14 12:53:05 +01:00
|
|
|
struct puffs_node *pn;
|
|
|
|
PUFFS_MAKECRED(pcr, &global_kcred);
|
|
|
|
|
2014-08-24 11:51:35 +02:00
|
|
|
if ((pn = puffs_pn_nodewalk(global_pu, 0, &ino_nr)) == NULL) {
|
|
|
|
lpuffs_debug("walk failed...\n");
|
2011-11-14 12:53:05 +01:00
|
|
|
return(EINVAL);
|
|
|
|
}
|
|
|
|
|
2014-08-24 11:51:35 +02:00
|
|
|
if (bytes > sizeof(rw_buf))
|
|
|
|
bytes = sizeof(rw_buf);
|
|
|
|
bytes_left = bytes;
|
2011-11-14 12:53:05 +01:00
|
|
|
|
2014-08-24 11:51:35 +02:00
|
|
|
if (global_pu->pu_ops.puffs_node_read == NULL)
|
|
|
|
return(EINVAL);
|
2011-11-14 12:53:05 +01:00
|
|
|
|
2014-08-24 11:51:35 +02:00
|
|
|
r = global_pu->pu_ops.puffs_node_read(global_pu, pn, (uint8_t *)rw_buf,
|
2011-11-14 12:53:05 +01:00
|
|
|
pos, &bytes_left, pcr, 0);
|
2014-08-24 11:51:35 +02:00
|
|
|
if (r) {
|
|
|
|
lpuffs_debug("puffs_node_read failed\n");
|
|
|
|
return(EINVAL);
|
2011-11-14 12:53:05 +01:00
|
|
|
}
|
|
|
|
|
2014-08-24 11:51:35 +02:00
|
|
|
bytes_done = bytes - bytes_left;
|
2011-11-14 12:53:05 +01:00
|
|
|
|
2014-08-24 11:51:35 +02:00
|
|
|
if (bytes_done > 0) {
|
|
|
|
if ((r = fsdriver_copyout(data, 0, rw_buf, bytes_done)) != OK)
|
|
|
|
return r;
|
|
|
|
update_timens(pn, ATIME, NULL);
|
|
|
|
}
|
2011-11-14 12:53:05 +01:00
|
|
|
|
2014-08-24 11:51:35 +02:00
|
|
|
return (ssize_t)bytes_done;
|
2011-11-14 12:53:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
2014-08-24 11:51:35 +02:00
|
|
|
* fs_write *
|
2011-11-14 12:53:05 +01:00
|
|
|
*===========================================================================*/
|
2014-08-24 11:51:35 +02:00
|
|
|
ssize_t fs_write(ino_t ino_nr, struct fsdriver_data *data, size_t bytes,
|
|
|
|
off_t pos, int call)
|
2011-11-14 12:53:05 +01:00
|
|
|
{
|
2014-08-24 11:51:35 +02:00
|
|
|
int r;
|
|
|
|
size_t bytes_left;
|
|
|
|
struct puffs_node *pn;
|
|
|
|
struct vattr va;
|
2014-08-24 12:00:06 +02:00
|
|
|
struct timespec cur_time;
|
2014-08-24 11:51:35 +02:00
|
|
|
PUFFS_MAKECRED(pcr, &global_kcred);
|
|
|
|
|
|
|
|
if ((pn = puffs_pn_nodewalk(global_pu, 0, &ino_nr)) == NULL) {
|
|
|
|
lpuffs_debug("walk failed...\n");
|
|
|
|
return(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytes > sizeof(rw_buf))
|
|
|
|
bytes = sizeof(rw_buf);
|
|
|
|
bytes_left = bytes;
|
|
|
|
|
|
|
|
/* At first try to change vattr */
|
|
|
|
if (global_pu->pu_ops.puffs_node_setattr == NULL)
|
|
|
|
return(EINVAL);
|
|
|
|
|
2014-08-24 12:00:06 +02:00
|
|
|
(void)clock_time(&cur_time);
|
|
|
|
|
2014-08-24 11:51:35 +02:00
|
|
|
puffs_vattr_null(&va);
|
|
|
|
if ((u_quad_t)(pos + bytes_left) > pn->pn_va.va_size)
|
|
|
|
va.va_size = bytes_left + pos;
|
2014-08-24 12:00:06 +02:00
|
|
|
va.va_ctime = va.va_mtime = cur_time;
|
2014-08-24 11:51:35 +02:00
|
|
|
va.va_atime = pn->pn_va.va_atime;
|
|
|
|
|
|
|
|
r = global_pu->pu_ops.puffs_node_setattr(global_pu, pn, &va, pcr);
|
|
|
|
if (r) return(EINVAL);
|
|
|
|
|
|
|
|
if ((r = fsdriver_copyin(data, 0, rw_buf, bytes)) != OK)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (global_pu->pu_ops.puffs_node_write == NULL)
|
|
|
|
return(EINVAL);
|
|
|
|
|
|
|
|
r = global_pu->pu_ops.puffs_node_write(global_pu, pn, (uint8_t *)rw_buf,
|
|
|
|
pos, &bytes_left, pcr, 0);
|
|
|
|
if (r != OK) return(EINVAL);
|
|
|
|
|
|
|
|
return (ssize_t)(bytes - bytes_left);
|
2011-11-14 12:53:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* fs_getdents *
|
|
|
|
*===========================================================================*/
|
2014-08-24 11:51:35 +02:00
|
|
|
ssize_t fs_getdents(ino_t ino_nr, struct fsdriver_data *data, size_t bytes,
|
|
|
|
off_t *pos)
|
2011-11-14 12:53:05 +01:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
register struct puffs_node *pn;
|
2014-08-24 11:51:35 +02:00
|
|
|
size_t buf_left, written;
|
2011-11-14 12:53:05 +01:00
|
|
|
struct dirent *dent;
|
|
|
|
int eofflag = 0;
|
|
|
|
PUFFS_MAKECRED(pcr, &global_kcred);
|
|
|
|
|
2014-08-24 11:51:35 +02:00
|
|
|
if ((pn = puffs_pn_nodewalk(global_pu, 0, &ino_nr)) == NULL) {
|
2011-11-14 12:53:05 +01:00
|
|
|
lpuffs_debug("walk failed...\n");
|
|
|
|
return(EINVAL);
|
|
|
|
}
|
|
|
|
|
2014-08-24 11:51:35 +02:00
|
|
|
if (bytes > sizeof(getdents_buf))
|
|
|
|
bytes = sizeof(getdents_buf);
|
|
|
|
memset(getdents_buf, 0, sizeof(getdents_buf)); /* Avoid leaking any data */
|
|
|
|
|
|
|
|
buf_left = bytes;
|
2011-11-14 12:53:05 +01:00
|
|
|
|
|
|
|
dent = (struct dirent*) getdents_buf;
|
|
|
|
|
2014-08-24 11:51:35 +02:00
|
|
|
r = global_pu->pu_ops.puffs_node_readdir(global_pu, pn, dent, pos,
|
2011-11-14 12:53:05 +01:00
|
|
|
&buf_left, pcr, &eofflag, 0, 0);
|
|
|
|
if (r) {
|
|
|
|
lpuffs_debug("puffs_node_readdir returned error\n");
|
|
|
|
return(EINVAL);
|
|
|
|
}
|
|
|
|
|
2014-08-24 11:51:35 +02:00
|
|
|
assert(buf_left <= bytes);
|
|
|
|
written = bytes - buf_left;
|
2011-11-14 12:53:05 +01:00
|
|
|
|
|
|
|
if (written == 0 && !eofflag) {
|
|
|
|
lpuffs_debug("The user's buffer is too small\n");
|
|
|
|
return(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (written) {
|
2014-08-24 11:51:35 +02:00
|
|
|
if ((r = fsdriver_copyout(data, 0, getdents_buf, written)) != OK)
|
|
|
|
return r;
|
2011-11-14 12:53:05 +01:00
|
|
|
}
|
|
|
|
|
2013-04-07 10:42:26 +02:00
|
|
|
update_timens(pn, ATIME, NULL);
|
2011-11-14 12:53:05 +01:00
|
|
|
|
2014-08-24 11:51:35 +02:00
|
|
|
/* The puffs readdir call has already updated the position. */
|
|
|
|
return written;
|
2011-11-14 12:53:05 +01:00
|
|
|
}
|