This patch will make mode_t 16-bits->32-bits, off_t 32-bits->64-bits,
ino_t 32-bits->64-bits, and nlink_t 16-bits->32-bits. As uid_t and
gid_t were already 32-bits we can drop the big_{mode,off,ino,uid,
gid,nlink}_t types from struct stat. This has huge consequences:
. lseek now has to accept a 64-bit argument and returns a 64-bit
result. Therefore we need a new lseek and return proper results
based on the used version (i.e., translate).
. some fcntl functions take and return a struct flock which holds 2
off_t fields. Like lseek, we need to distinguish between old and
new versions.
. getdents uses struct dirent which has among other fields an ino_t
and off_t field. Consequently we need a new getdents system call
and have to translate when we encounter an old call.
. truncate and ftruncate also take off_t arguments and therefore
require a new version.
To add insult to injury VFS itself uses getdents. This has as
consequence that we have 2^3 combinations of version for getdents; old
and new version of libc (new system calls), new and old version of VFS
(when booting an old image), old and new version of an FS. We can
distinguish between these cases as follows; libc will use new system
call numbers that have not yet been in use. When using a new number,
VFS knows libc is new, otherwise it's an old libc. Vice versa, libc
first tries the new number and if that fails due to ENOSYS (unknown
system call number) it knows it's dealing with an old VFS. Finally,
since we now have VFS-FS protocol versioning and by bumping the version
to 1, VFS knows an FS is old when it reports version 0 and an FS knows
VFS is old when VFS reports it's using version 0.
0 = old version, 1 = new version
libc | VFS | FS | approach
----------------------------
0 | 0 | 0 | Nothing to do
----------------------------
0 | 0 | 1 | VFS reports protocol version 0, FS falls back
| to using the old getdents function
----------------------------
0 | 1 | 0 | VFS reports protocol version 1, FS version 0.
| libc requires old getdents so there is nothing
| to do. VFS itself needs new getdents and will
| translate.
----------------------------
0 | 1 | 1 | VFS reports protocol version 1, FS version 1.
| VFS intercepts getdents results and translates
| them on the fly before returning results to libc
----------------------------
1 | 0 | 0 | libc first tries new system call number and
| then fall back to the old number. libc first
| translates the results to the new getdents before
| returning.
----------------------------
1 | 0 | 1 | VFS reports protocol version 0, so FS uses fall
| back. libc translates.
----------------------------
1 | 1 | 0 | VFS reports protocol version 1, FS version 0. VFS
| will intercept all results and translate on the
| fly.
----------------------------
1 | 1 | 1 | Nothing to do.
As VFS can distinguish between old and new system calls of (f)truncate,
lseek, and fcntl, it will translate the arguments and results if
necessary. Likewise libc falls back to using the old calls and
translating the results to the new version if necessary.
Change-Id: Id5bc819e6ef90f317dbddb21020e0a65028225ce
366 lines
8.4 KiB
C
366 lines
8.4 KiB
C
/* VTreeFS - read.c - by Alen Stojanov and David van Moolenbroek */
|
|
|
|
#include "inc.h"
|
|
#include <dirent.h>
|
|
|
|
#define GETDENTS_BUFSIZ 4096
|
|
#define DWORD_ALIGN(len) (((len) + sizeof(long) - 1) & ~(sizeof(long) - 1))
|
|
|
|
/*===========================================================================*
|
|
* fs_read *
|
|
*===========================================================================*/
|
|
int fs_read(void)
|
|
{
|
|
/* Read from a file.
|
|
*/
|
|
cp_grant_id_t gid;
|
|
struct inode *node;
|
|
off_t pos;
|
|
size_t len;
|
|
char *ptr;
|
|
int r;
|
|
|
|
if (fs_m_in.REQ_SEEK_POS_HI != 0)
|
|
return EIO;
|
|
|
|
/* Try to get inode by to its inode number. */
|
|
if ((node = find_inode(fs_m_in.REQ_INODE_NR)) == NULL)
|
|
return EINVAL;
|
|
|
|
/* Check whether the node is a regular file. */
|
|
if (!S_ISREG(node->i_stat.mode))
|
|
return EINVAL;
|
|
|
|
/* Get the values from the request message. */
|
|
gid = fs_m_in.REQ_GRANT;
|
|
pos = fs_m_in.REQ_SEEK_POS_LO;
|
|
|
|
/* Call the read hook, if any. */
|
|
if (!is_inode_deleted(node) && vtreefs_hooks->read_hook != NULL) {
|
|
len = fs_m_in.REQ_NBYTES;
|
|
|
|
/* On success, the read hook provides us with a pointer to the
|
|
* resulting data. This avoids copying overhead.
|
|
*/
|
|
r = vtreefs_hooks->read_hook(node, pos, &ptr, &len,
|
|
get_inode_cbdata(node));
|
|
|
|
assert(len <= fs_m_in.REQ_NBYTES);
|
|
|
|
/* Copy the resulting data to user space. */
|
|
if (r == OK && len > 0) {
|
|
r = sys_safecopyto(fs_m_in.m_source, fs_m_in.REQ_GRANT,
|
|
0, (vir_bytes) ptr, len);
|
|
}
|
|
} else {
|
|
/* Feign an empty file. */
|
|
r = OK;
|
|
len = 0;
|
|
}
|
|
|
|
if (r == OK) {
|
|
fs_m_out.RES_SEEK_POS_HI = 0;
|
|
fs_m_out.RES_SEEK_POS_LO = pos + len;
|
|
fs_m_out.RES_NBYTES = len;
|
|
}
|
|
|
|
return r;
|
|
}
|
|
|
|
/*===========================================================================*
|
|
* fs_getdents_321 *
|
|
*===========================================================================*/
|
|
int fs_getdents_321(void)
|
|
{
|
|
/* Retrieve directory entries.
|
|
*/
|
|
struct inode *node, *child = NULL;
|
|
struct dirent_321 *dent;
|
|
char *name;
|
|
size_t len, off, user_off, user_left;
|
|
off_t pos;
|
|
int r, skip, get_next, indexed;
|
|
static char buf[GETDENTS_BUFSIZ];
|
|
|
|
if (fs_m_in.REQ_SEEK_POS_HI != 0)
|
|
return EIO;
|
|
|
|
if ((node = find_inode(fs_m_in.REQ_INODE_NR)) == NULL)
|
|
return EINVAL;
|
|
|
|
off = 0;
|
|
user_off = 0;
|
|
user_left = fs_m_in.REQ_MEM_SIZE;
|
|
indexed = node->i_indexed;
|
|
get_next = FALSE;
|
|
child = NULL;
|
|
|
|
/* Call the getdents hook, if any, to "refresh" the directory. */
|
|
if (!is_inode_deleted(node) && vtreefs_hooks->getdents_hook != NULL) {
|
|
r = vtreefs_hooks->getdents_hook(node, get_inode_cbdata(node));
|
|
if (r != OK) return r;
|
|
}
|
|
|
|
for (pos = fs_m_in.REQ_SEEK_POS_LO; ; pos++) {
|
|
/* Determine which inode and name to use for this entry. */
|
|
if (pos == 0) {
|
|
/* The "." entry. */
|
|
child = node;
|
|
name = ".";
|
|
}
|
|
else if (pos == 1) {
|
|
/* The ".." entry. */
|
|
child = get_parent_inode(node);
|
|
if (child == NULL)
|
|
child = node;
|
|
name = "..";
|
|
}
|
|
else if (pos - 2 < indexed) {
|
|
/* All indexed entries. */
|
|
child = get_inode_by_index(node, pos - 2);
|
|
|
|
/* If there is no inode with this particular index,
|
|
* continue with the next index number.
|
|
*/
|
|
if (child == NULL) continue;
|
|
|
|
name = child->i_name;
|
|
}
|
|
else {
|
|
/* All non-indexed entries. */
|
|
|
|
/* If this is the first loop iteration, first get to
|
|
* the non-indexed child identified by the current
|
|
* position.
|
|
*/
|
|
if (get_next == FALSE) {
|
|
skip = pos - indexed - 2;
|
|
child = get_first_inode(node);
|
|
|
|
/* Skip indexed children. */
|
|
while (child != NULL &&
|
|
child->i_index != NO_INDEX)
|
|
child = get_next_inode(child);
|
|
|
|
/* Skip to the right position. */
|
|
while (child != NULL && skip-- > 0)
|
|
child = get_next_inode(child);
|
|
|
|
get_next = TRUE;
|
|
}
|
|
else {
|
|
child = get_next_inode(child);
|
|
}
|
|
|
|
/* No more children? Then stop. */
|
|
if (child == NULL)
|
|
break;
|
|
|
|
assert(!is_inode_deleted(child));
|
|
|
|
name = child->i_name;
|
|
}
|
|
|
|
len = DWORD_ALIGN(sizeof(struct dirent_321) + strlen(name));
|
|
|
|
/* Is the user buffer too small to store another record? */
|
|
if (user_off + off + len > user_left) {
|
|
/* Is the user buffer too small for even a single
|
|
* record?
|
|
*/
|
|
if (user_off == 0 && off == 0)
|
|
return EINVAL;
|
|
|
|
break;
|
|
}
|
|
|
|
/* If our own buffer cannot contain the new record, copy out
|
|
* first.
|
|
*/
|
|
if (off + len > sizeof(buf)) {
|
|
r = sys_safecopyto(fs_m_in.m_source, fs_m_in.REQ_GRANT,
|
|
user_off, (vir_bytes) buf, off);
|
|
if (r != OK) return r;
|
|
|
|
user_off += off;
|
|
user_left -= off;
|
|
off = 0;
|
|
}
|
|
|
|
/* Fill in the actual directory entry. */
|
|
dent = (struct dirent_321 *) &buf[off];
|
|
dent->d_ino = (u32_t) get_inode_number(child);
|
|
dent->d_off = (i32_t) pos;
|
|
dent->d_reclen = len;
|
|
strcpy(dent->d_name, name);
|
|
|
|
off += len;
|
|
}
|
|
|
|
/* If there is anything left in our own buffer, copy that out now. */
|
|
if (off > 0) {
|
|
r = sys_safecopyto(fs_m_in.m_source, fs_m_in.REQ_GRANT,
|
|
user_off, (vir_bytes) buf, off);
|
|
if (r != OK)
|
|
return r;
|
|
|
|
user_off += off;
|
|
}
|
|
|
|
fs_m_out.RES_SEEK_POS_HI = 0;
|
|
fs_m_out.RES_SEEK_POS_LO = pos;
|
|
fs_m_out.RES_NBYTES = user_off;
|
|
|
|
return OK;
|
|
}
|
|
|
|
/*===========================================================================*
|
|
* fs_getdents *
|
|
*===========================================================================*/
|
|
int fs_getdents(void)
|
|
{
|
|
/* Retrieve directory entries.
|
|
*/
|
|
struct inode *node, *child = NULL;
|
|
struct dirent *dent;
|
|
char *name;
|
|
size_t len, off, user_off, user_left;
|
|
off_t pos;
|
|
int r, skip, get_next, indexed;
|
|
static char buf[GETDENTS_BUFSIZ];
|
|
|
|
if (proto_version == 0)
|
|
return fs_getdents_321();
|
|
|
|
if (fs_m_in.REQ_SEEK_POS_HI != 0)
|
|
return EIO;
|
|
|
|
if ((node = find_inode(fs_m_in.REQ_INODE_NR)) == NULL)
|
|
return EINVAL;
|
|
|
|
off = 0;
|
|
user_off = 0;
|
|
user_left = fs_m_in.REQ_MEM_SIZE;
|
|
indexed = node->i_indexed;
|
|
get_next = FALSE;
|
|
child = NULL;
|
|
|
|
/* Call the getdents hook, if any, to "refresh" the directory. */
|
|
if (!is_inode_deleted(node) && vtreefs_hooks->getdents_hook != NULL) {
|
|
r = vtreefs_hooks->getdents_hook(node, get_inode_cbdata(node));
|
|
if (r != OK) return r;
|
|
}
|
|
|
|
for (pos = fs_m_in.REQ_SEEK_POS_LO; ; pos++) {
|
|
/* Determine which inode and name to use for this entry. */
|
|
if (pos == 0) {
|
|
/* The "." entry. */
|
|
child = node;
|
|
name = ".";
|
|
}
|
|
else if (pos == 1) {
|
|
/* The ".." entry. */
|
|
child = get_parent_inode(node);
|
|
if (child == NULL)
|
|
child = node;
|
|
name = "..";
|
|
}
|
|
else if (pos - 2 < indexed) {
|
|
/* All indexed entries. */
|
|
child = get_inode_by_index(node, pos - 2);
|
|
|
|
/* If there is no inode with this particular index,
|
|
* continue with the next index number.
|
|
*/
|
|
if (child == NULL) continue;
|
|
|
|
name = child->i_name;
|
|
}
|
|
else {
|
|
/* All non-indexed entries. */
|
|
|
|
/* If this is the first loop iteration, first get to
|
|
* the non-indexed child identified by the current
|
|
* position.
|
|
*/
|
|
if (get_next == FALSE) {
|
|
skip = pos - indexed - 2;
|
|
child = get_first_inode(node);
|
|
|
|
/* Skip indexed children. */
|
|
while (child != NULL &&
|
|
child->i_index != NO_INDEX)
|
|
child = get_next_inode(child);
|
|
|
|
/* Skip to the right position. */
|
|
while (child != NULL && skip-- > 0)
|
|
child = get_next_inode(child);
|
|
|
|
get_next = TRUE;
|
|
}
|
|
else {
|
|
child = get_next_inode(child);
|
|
}
|
|
|
|
/* No more children? Then stop. */
|
|
if (child == NULL)
|
|
break;
|
|
|
|
assert(!is_inode_deleted(child));
|
|
|
|
name = child->i_name;
|
|
}
|
|
|
|
len = DWORD_ALIGN(sizeof(struct dirent) + strlen(name));
|
|
|
|
/* Is the user buffer too small to store another record? */
|
|
if (user_off + off + len > user_left) {
|
|
/* Is the user buffer too small for even a single
|
|
* record?
|
|
*/
|
|
if (user_off == 0 && off == 0)
|
|
return EINVAL;
|
|
|
|
break;
|
|
}
|
|
|
|
/* If our own buffer cannot contain the new record, copy out
|
|
* first.
|
|
*/
|
|
if (off + len > sizeof(buf)) {
|
|
r = sys_safecopyto(fs_m_in.m_source, fs_m_in.REQ_GRANT,
|
|
user_off, (vir_bytes) buf, off);
|
|
if (r != OK) return r;
|
|
|
|
user_off += off;
|
|
user_left -= off;
|
|
off = 0;
|
|
}
|
|
|
|
/* Fill in the actual directory entry. */
|
|
dent = (struct dirent *) &buf[off];
|
|
dent->d_ino = (ino_t) get_inode_number(child);
|
|
dent->d_off = (off_t) pos;
|
|
dent->d_reclen = len;
|
|
strcpy(dent->d_name, name);
|
|
|
|
off += len;
|
|
}
|
|
|
|
/* If there is anything left in our own buffer, copy that out now. */
|
|
if (off > 0) {
|
|
r = sys_safecopyto(fs_m_in.m_source, fs_m_in.REQ_GRANT,
|
|
user_off, (vir_bytes) buf, off);
|
|
if (r != OK)
|
|
return r;
|
|
|
|
user_off += off;
|
|
}
|
|
|
|
fs_m_out.RES_SEEK_POS_HI = 0;
|
|
fs_m_out.RES_SEEK_POS_LO = pos;
|
|
fs_m_out.RES_NBYTES = user_off;
|
|
|
|
return OK;
|
|
}
|