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