jdk/src/solaris/native/sun/nio/ch/FileChannelImpl.c
author alanb
Thu, 13 Mar 2008 19:29:41 +0000
changeset 68 89f4f4a43855
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
6628575: (fc) lock/tryLock methods do not work with NFS servers that limit lock range to max file size Reviewed-by: sherman
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <sys/mman.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <sys/stat.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "sun_nio_ch_FileChannelImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "java_lang_Integer.h"
68
89f4f4a43855 6628575: (fc) lock/tryLock methods do not work with NFS servers that limit lock range to max file size
alanb
parents: 2
diff changeset
    34
#include "java_lang_Long.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "nio.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "nio_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <dlfcn.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
static jfieldID chan_fd;        /* jobject 'fd' in sun.io.FileChannelImpl */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
typedef struct sendfilevec64 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    int     sfv_fd;         /* input fd */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    uint_t  sfv_flag;       /* Flags. see below */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    off64_t sfv_off;        /* offset to start reading from */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    size_t  sfv_len;        /* amount of data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
} sendfilevec_t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/* Function pointer for sendfilev on Solaris 8+ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
typedef ssize_t sendfile_func(int fildes, const struct sendfilevec64 *vec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                              int sfvcnt, size_t *xferred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
sendfile_func* my_sendfile_func = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
#include <sys/sendfile.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
/* Function pointer for sendfile64 on Linux 2.6 (and newer 2.4 kernels) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
typedef ssize_t sendfile64_func(int out_fd, int in_fd, off64_t *offset, size_t count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
sendfile64_func* my_sendfile64_func = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
Java_sun_nio_ch_FileChannelImpl_initIDs(JNIEnv *env, jclass clazz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    jlong pageSize = sysconf(_SC_PAGESIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    chan_fd = (*env)->GetFieldID(env, clazz, "fd", "Ljava/io/FileDescriptor;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    if (dlopen("/usr/lib/libsendfile.so.1", RTLD_GLOBAL | RTLD_LAZY) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        my_sendfile_func = (sendfile_func*) dlsym(RTLD_DEFAULT, "sendfilev64");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    my_sendfile64_func = (sendfile64_func*) dlsym(RTLD_DEFAULT, "sendfile64");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    return pageSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
static jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
handle(JNIEnv *env, jlong rv, char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    if (rv >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    if (errno == EINTR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return IOS_INTERRUPTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    JNU_ThrowIOExceptionWithLastError(env, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
Java_sun_nio_ch_FileChannelImpl_map0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                     jint prot, jlong off, jlong len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    void *mapAddress = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    jobject fdo = (*env)->GetObjectField(env, this, chan_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    int protections = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    int flags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    if (prot == sun_nio_ch_FileChannelImpl_MAP_RO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        protections = PROT_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        flags = MAP_SHARED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    } else if (prot == sun_nio_ch_FileChannelImpl_MAP_RW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        protections = PROT_WRITE | PROT_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        flags = MAP_SHARED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    } else if (prot == sun_nio_ch_FileChannelImpl_MAP_PV) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        protections =  PROT_WRITE | PROT_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        flags = MAP_PRIVATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    mapAddress = mmap64(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        0,                    /* Let OS decide location */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        len,                  /* Number of bytes to map */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        protections,          /* File permissions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        flags,                /* Changes are shared */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        fd,                   /* File descriptor of mapped file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        off);                 /* Offset into file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    if (mapAddress == MAP_FAILED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (errno == ENOMEM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            JNU_ThrowOutOfMemoryError(env, "Map failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return handle(env, -1, "Map failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    return ((jlong) (unsigned long) mapAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
Java_sun_nio_ch_FileChannelImpl_unmap0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                       jlong address, jlong len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    void *a = (void *)jlong_to_ptr(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    return handle(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                  munmap(a, (size_t)len),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                  "Unmap failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
Java_sun_nio_ch_FileChannelImpl_truncate0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                          jobject fdo, jlong size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    return handle(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                  ftruncate64(fdval(env, fdo), size),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                  "Truncation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
Java_sun_nio_ch_FileChannelImpl_force0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                       jobject fdo, jboolean md)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    if (md == JNI_FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        result = fdatasync(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        result = fsync(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    return handle(env, result, "Force failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
Java_sun_nio_ch_FileChannelImpl_position0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                          jobject fdo, jlong offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    jlong result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    if (offset < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        result = lseek64(fd, 0, SEEK_CUR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        result = lseek64(fd, offset, SEEK_SET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    return handle(env, result, "Position failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
Java_sun_nio_ch_FileChannelImpl_size0(JNIEnv *env, jobject this, jobject fdo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    struct stat64 fbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    if (fstat64(fdval(env, fdo), &fbuf) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return handle(env, -1, "Size failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    return fbuf.st_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
Java_sun_nio_ch_FileChannelImpl_close0(JNIEnv *env, jobject this, jobject fdo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    if (fd != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        jlong result = close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (result < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            JNU_ThrowIOExceptionWithLastError(env, "Close failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                            jint srcFD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                            jlong position, jlong count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                            jint dstFD)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    jlong max = (jlong)java_lang_Integer_MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    jlong n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    if (my_sendfile64_func == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        off_t offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (position > max)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            return IOS_UNSUPPORTED_CASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (count > max)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            count = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        offset = (off_t)position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        n = sendfile(dstFD, srcFD, &offset, (size_t)count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        off64_t offset = (off64_t)position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        n = (*my_sendfile64_func)(dstFD, srcFD, &offset, (size_t)count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (errno == EAGAIN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if ((errno == EINVAL) && ((ssize_t)count >= 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return IOS_UNSUPPORTED_CASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (errno == EINTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            return IOS_INTERRUPTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    if (my_sendfile_func == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return IOS_UNSUPPORTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        sendfilevec_t sfv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        size_t numBytes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        jlong result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        sfv.sfv_fd = srcFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        sfv.sfv_flag = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        sfv.sfv_off = (off64_t)position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        sfv.sfv_len = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        result = (*my_sendfile_func)(dstFD, &sfv, 1, &numBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        /* Solaris sendfilev() will return -1 even if some bytes have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
         * transferred, so we check numBytes first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (numBytes > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return numBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (result < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            if (errno == EAGAIN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if ((errno == EINVAL) && ((ssize_t)count >= 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                return IOS_UNSUPPORTED_CASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (errno == EINTR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                return IOS_INTERRUPTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            JNU_ThrowIOExceptionWithLastError(env, "Transfer failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
Java_sun_nio_ch_FileChannelImpl_lock0(JNIEnv *env, jobject this, jobject fdo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                      jboolean block, jlong pos, jlong size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                      jboolean shared)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    jint lockResult = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    int cmd = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    struct flock64 fl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    fl.l_whence = SEEK_SET;
68
89f4f4a43855 6628575: (fc) lock/tryLock methods do not work with NFS servers that limit lock range to max file size
alanb
parents: 2
diff changeset
   295
    if (size == (jlong)java_lang_Long_MAX_VALUE) {
89f4f4a43855 6628575: (fc) lock/tryLock methods do not work with NFS servers that limit lock range to max file size
alanb
parents: 2
diff changeset
   296
        fl.l_len = (off64_t)0;
89f4f4a43855 6628575: (fc) lock/tryLock methods do not work with NFS servers that limit lock range to max file size
alanb
parents: 2
diff changeset
   297
    } else {
89f4f4a43855 6628575: (fc) lock/tryLock methods do not work with NFS servers that limit lock range to max file size
alanb
parents: 2
diff changeset
   298
        fl.l_len = (off64_t)size;
89f4f4a43855 6628575: (fc) lock/tryLock methods do not work with NFS servers that limit lock range to max file size
alanb
parents: 2
diff changeset
   299
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    fl.l_start = (off64_t)pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    if (shared == JNI_TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        fl.l_type = F_RDLCK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        fl.l_type = F_WRLCK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    if (block == JNI_TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        cmd = F_SETLKW64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        cmd = F_SETLK64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    lockResult = fcntl(fd, cmd, &fl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    if (lockResult < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if ((cmd == F_SETLK64) && (errno == EAGAIN))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            return sun_nio_ch_FileChannelImpl_NO_LOCK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (errno == EINTR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            return sun_nio_ch_FileChannelImpl_INTERRUPTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        JNU_ThrowIOExceptionWithLastError(env, "Lock failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
Java_sun_nio_ch_FileChannelImpl_release0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                         jobject fdo, jlong pos, jlong size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    jint lockResult = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    struct flock64 fl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    int cmd = F_SETLK64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    fl.l_whence = SEEK_SET;
68
89f4f4a43855 6628575: (fc) lock/tryLock methods do not work with NFS servers that limit lock range to max file size
alanb
parents: 2
diff changeset
   333
    if (size == (jlong)java_lang_Long_MAX_VALUE) {
89f4f4a43855 6628575: (fc) lock/tryLock methods do not work with NFS servers that limit lock range to max file size
alanb
parents: 2
diff changeset
   334
        fl.l_len = (off64_t)0;
89f4f4a43855 6628575: (fc) lock/tryLock methods do not work with NFS servers that limit lock range to max file size
alanb
parents: 2
diff changeset
   335
    } else {
89f4f4a43855 6628575: (fc) lock/tryLock methods do not work with NFS servers that limit lock range to max file size
alanb
parents: 2
diff changeset
   336
        fl.l_len = (off64_t)size;
89f4f4a43855 6628575: (fc) lock/tryLock methods do not work with NFS servers that limit lock range to max file size
alanb
parents: 2
diff changeset
   337
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    fl.l_start = (off64_t)pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    fl.l_type = F_UNLCK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    lockResult = fcntl(fd, cmd, &fl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    if (lockResult < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        JNU_ThrowIOExceptionWithLastError(env, "Release failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
}