8164900: Add support for O_DIRECT

Add support for Direct I/O in FileChannel

Co-authored-by: Volker Simonis <volker.simonis@gmail.com>
Reviewed-by: alanb, bpb, alanbur, coffeys, aph, clanger, plevart, mli, psandoz, simonis
This commit is contained in:
Lucy Lu 2017-10-17 16:51:11 -07:00 committed by Brian Burkhalter
parent 97db013bd3
commit ec1c3bce45
30 changed files with 1523 additions and 45 deletions

View file

@ -34,6 +34,7 @@
#include <fcntl.h>
#include <sys/uio.h>
#include <unistd.h>
#include <sys/statvfs.h>
#if defined(__linux__)
#include <linux/fs.h>
#include <sys/ioctl.h>
@ -323,3 +324,58 @@ Java_sun_nio_ch_FileDispatcherImpl_closeIntFD(JNIEnv *env, jclass clazz, jint fd
{
closeFileDescriptor(env, fd);
}
JNIEXPORT jint JNICALL
Java_sun_nio_ch_FileDispatcherImpl_setDirect0(JNIEnv *env, jclass clazz,
jobject fdo)
{
jint fd = fdval(env, fdo);
jint result;
#ifdef MACOSX
struct statvfs file_stat;
#else
struct statvfs64 file_stat;
#endif
#if defined(O_DIRECT) || defined(F_NOCACHE) || defined(DIRECTIO_ON)
#ifdef O_DIRECT
jint orig_flag;
orig_flag = fcntl(fd, F_GETFL);
if (orig_flag == -1) {
JNU_ThrowIOExceptionWithLastError(env, "DirectIO setup failed");
return -1;
}
result = fcntl(fd, F_SETFL, orig_flag | O_DIRECT);
if (result == -1) {
JNU_ThrowIOExceptionWithLastError(env, "DirectIO setup failed");
return result;
}
#elif F_NOCACHE
result = fcntl(fd, F_NOCACHE, 1);
if (result == -1) {
JNU_ThrowIOExceptionWithLastError(env, "DirectIO setup failed");
return result;
}
#elif DIRECTIO_ON
result = directio(fd, DIRECTIO_ON);
if (result == -1) {
JNU_ThrowIOExceptionWithLastError(env, "DirectIO setup failed");
return result;
}
#endif
#ifdef MACOSX
result = fstatvfs(fd, &file_stat);
#else
result = fstatvfs64(fd, &file_stat);
#endif
if(result == -1) {
JNU_ThrowIOExceptionWithLastError(env, "DirectIO setup failed");
return result;
} else {
result = (int)file_stat.f_frsize;
}
#else
result == -1;
#endif
return result;
}