< prev index next >

src/java.base/unix/native/libnio/ch/FileDispatcherImpl.c

Print this page
rev 16196 : 8168628: (fc) SIGBUS when extending file size to map it
Summary: Synchronize file extension and subsequent map0(); on Linux use fallocate64() instead of ftruncate64().
Reviewed-by: rehn, simonis, alanb
   1 /*
   2  * Copyright (c) 2000, 2012, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 166          * 'writable' attribute of the corresponding file channel so we have to
 167          * use 'fcntl'.
 168          */
 169         int getfl = fcntl(fd, F_GETFL);
 170         if (getfl >= 0 && (getfl & O_ACCMODE) == O_RDONLY) {
 171             return 0;
 172         }
 173 #endif /* _AIX */
 174         result = fsync(fd);
 175     }
 176 #endif /* not-MACOSX */
 177     return handle(env, result, "Force failed");
 178 }
 179 
 180 JNIEXPORT jint JNICALL
 181 Java_sun_nio_ch_FileDispatcherImpl_truncate0(JNIEnv *env, jobject this,
 182                                              jobject fdo, jlong size)
 183 {
 184     return handle(env,
 185                   ftruncate64(fdval(env, fdo), size),

























 186                   "Truncation failed");
 187 }
 188 
 189 JNIEXPORT jlong JNICALL
 190 Java_sun_nio_ch_FileDispatcherImpl_size0(JNIEnv *env, jobject this, jobject fdo)
 191 {
 192     jint fd = fdval(env, fdo);
 193     struct stat64 fbuf;
 194 
 195     if (fstat64(fd, &fbuf) < 0)
 196         return handle(env, -1, "Size failed");
 197 
 198 #ifdef BLKGETSIZE64
 199     if (S_ISBLK(fbuf.st_mode)) {
 200         uint64_t size;
 201         if (ioctl(fd, BLKGETSIZE64, &size) < 0)
 202             return handle(env, -1, "Size failed");
 203         return (jlong)size;
 204     }
 205 #endif


   1 /*
   2  * Copyright (c) 2000, 2016, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 166          * 'writable' attribute of the corresponding file channel so we have to
 167          * use 'fcntl'.
 168          */
 169         int getfl = fcntl(fd, F_GETFL);
 170         if (getfl >= 0 && (getfl & O_ACCMODE) == O_RDONLY) {
 171             return 0;
 172         }
 173 #endif /* _AIX */
 174         result = fsync(fd);
 175     }
 176 #endif /* not-MACOSX */
 177     return handle(env, result, "Force failed");
 178 }
 179 
 180 JNIEXPORT jint JNICALL
 181 Java_sun_nio_ch_FileDispatcherImpl_truncate0(JNIEnv *env, jobject this,
 182                                              jobject fdo, jlong size)
 183 {
 184     return handle(env,
 185                   ftruncate64(fdval(env, fdo), size),
 186                   "Truncation failed");
 187 }
 188 
 189 JNIEXPORT jint JNICALL
 190 Java_sun_nio_ch_FileDispatcherImpl_allocate0(JNIEnv *env, jobject this,
 191                                              jobject fdo, jlong size)
 192 {
 193     jint fd = fdval(env, fdo);
 194 #if defined(__linux__)
 195     /*
 196      * On Linux, if the file size is being increased, then ftruncate64()
 197      * will modify the metadata value of the size without actually allocating
 198      * any blocks which can cause a SIGBUS error if the file is subsequently
 199      * memory-mapped.
 200      */
 201     struct stat64 fbuf;
 202 
 203     if (fstat64(fd, &fbuf) == 0 && size > fbuf.st_blocks*512) {
 204         return handle(env,
 205                       fallocate64(fd, 0, 0, size),
 206                       "Allocation failed");
 207     }
 208 #endif
 209     return handle(env,
 210                   ftruncate64(fd, size),
 211                   "Truncation failed");
 212 }
 213 
 214 JNIEXPORT jlong JNICALL
 215 Java_sun_nio_ch_FileDispatcherImpl_size0(JNIEnv *env, jobject this, jobject fdo)
 216 {
 217     jint fd = fdval(env, fdo);
 218     struct stat64 fbuf;
 219 
 220     if (fstat64(fd, &fbuf) < 0)
 221         return handle(env, -1, "Size failed");
 222 
 223 #ifdef BLKGETSIZE64
 224     if (S_ISBLK(fbuf.st_mode)) {
 225         uint64_t size;
 226         if (ioctl(fd, BLKGETSIZE64, &size) < 0)
 227             return handle(env, -1, "Size failed");
 228         return (jlong)size;
 229     }
 230 #endif


< prev index next >