1 /* Part of libvboxfs - (c) 2012, D.C. van Moolenbroek */
6 * We perform all file I/O using a local, intermediate buffer. While in theory
7 * it would be possible to perform direct DMA from/to the user process, this
8 * does not work in practice: on short reads, VirtualBox copies back the entire
9 * provided buffer rather than only the part actually filled, resulting in the
10 * unused part of the buffer being clobbered. Marking the buffer as bi-
11 * directional would solve this, except it would also eliminate all the
12 * zero-copy benefits for reads; in addition, it is prevented by the protection
13 * set on the given grant.
16 #define VBOXFS_MAX_FILEIO 65536 /* maximum I/O chunk size */
18 static char iobuf
[VBOXFS_MAX_FILEIO
];
24 vboxfs_open(char *path
, int flags
, int mode
, sffs_file_t
*handle
)
26 vboxfs_handle_t
*handlep
;
29 handlep
= (vboxfs_handle_t
*) malloc(sizeof(*handlep
));
31 if ((r
= vboxfs_open_file(path
, flags
, mode
, handlep
, NULL
)) != OK
) {
37 *handle
= (sffs_file_t
) handlep
;
43 * Read or write a chunk from or to a file.
46 read_write(vboxfs_handle_t handle
, char *buf
, size_t size
, u64_t pos
,
49 vbox_param_t param
[5];
52 dir
= write
? VBOX_DIR_OUT
: VBOX_DIR_IN
;
53 call
= write
? VBOXFS_CALL_WRITE
: VBOXFS_CALL_READ
;
55 vbox_set_u32(¶m
[0], vboxfs_root
);
56 vbox_set_u64(¶m
[1], handle
);
57 vbox_set_u64(¶m
[2], pos
);
58 vbox_set_u32(¶m
[3], size
);
59 vbox_set_ptr(¶m
[4], buf
, size
, dir
);
61 if ((r
= vbox_call(vboxfs_conn
, call
, param
, 5, NULL
)) != OK
)
64 return vbox_get_u32(¶m
[3]);
71 vboxfs_read(sffs_file_t handle
, char *buf
, size_t size
, u64_t pos
)
73 vboxfs_handle_t
*handlep
;
75 handlep
= (vboxfs_handle_t
*) handle
;
77 return read_write(*handlep
, buf
, size
, pos
, FALSE
/*write*/);
84 vboxfs_write(sffs_file_t handle
, char *buf
, size_t len
, u64_t pos
)
86 vboxfs_handle_t
*handlep
;
88 handlep
= (vboxfs_handle_t
*) handle
;
90 return read_write(*handlep
, buf
, len
, pos
, TRUE
/*write*/);
94 * Close a file handle.
97 vboxfs_close(sffs_file_t handle
)
99 vboxfs_handle_t
*handlep
;
101 handlep
= (vboxfs_handle_t
*) handle
;
103 vboxfs_close_file(*handlep
);
111 * Return an internal buffer address and size for I/O operations.
114 vboxfs_buffer(char **ptr
)
118 return sizeof(iobuf
);