00001
00002
00003
00004
00005
00006
00007
00008 #include "adio.h"
00009
00010 #ifdef ROMIO_NTFS
00011
00012
00013 #include "..\ad_ntfs\ad_ntfs.h"
00014 int ADIOI_Set_lock(FDTYPE fd, int cmd, int type, ADIO_Offset offset, int whence,
00015 ADIO_Offset len)
00016 {
00017 static char myname[] = "ADIOI_Set_lock";
00018 int ret_val, error_code = MPI_SUCCESS;
00019 OVERLAPPED Overlapped;
00020 DWORD dwFlags;
00021
00022 ADIOI_UNREFERENCED_ARG(whence);
00023
00024 if (len == 0) return MPI_SUCCESS;
00025
00026 dwFlags = type;
00027
00028 Overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
00029 #ifdef HAVE_INT64
00030 Overlapped.Offset = ( (DWORD) ( offset & (__int64) 0xFFFFFFFF ) );
00031 Overlapped.OffsetHigh = ( (DWORD) ( (offset >> 32) & (__int64) 0xFFFFFFFF ) );
00032
00033 if (cmd == ADIOI_LOCK_CMD)
00034 {
00035
00036 ret_val = LockFileEx(fd, dwFlags, 0,
00037 ( (DWORD) ( len & (__int64) 0xFFFFFFFF ) ),
00038 ( (DWORD) ( (len >> 32) & (__int64) 0xFFFFFFFF ) ),
00039 &Overlapped);
00040 }
00041 else
00042 {
00043
00044 ret_val = UnlockFileEx(fd, 0,
00045 ( (DWORD) ( len & (__int64) 0xFFFFFFFF ) ),
00046 ( (DWORD) ( (len >> 32) & (__int64) 0xFFFFFFFF ) ),
00047 &Overlapped);
00048 }
00049 #else
00050 Overlapped.Offset = offset;
00051 Overlapped.OffsetHigh = 0;
00052
00053 if (cmd == ADIOI_LOCK_CMD)
00054 {
00055
00056 ret_val = LockFileEx(fd, dwFlags, 0, len, 0, &Overlapped);
00057 }
00058 else
00059 {
00060
00061 ret_val = UnlockFileEx(fd, 0, len, 0, &Overlapped);
00062 }
00063 #endif
00064
00065 if (!ret_val)
00066 {
00067 char errMsg[ADIOI_NTFS_ERR_MSG_MAX];
00068
00069
00070
00071
00072 ret_val = GetLastError();
00073 if (ret_val == ERROR_IO_PENDING)
00074 {
00075 DWORD dummy;
00076 ret_val = GetOverlappedResult(fd, &Overlapped, &dummy, TRUE);
00077 if (ret_val)
00078 {
00079 CloseHandle(Overlapped.hEvent);
00080 return MPI_SUCCESS;
00081 }
00082 ret_val = GetLastError();
00083 }
00084 ADIOI_NTFS_Strerror(ret_val, errMsg, ADIOI_NTFS_ERR_MSG_MAX);
00085 error_code = MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE, myname, __LINE__,
00086 MPI_ERR_IO, "**io", "**io %s", errMsg);
00087 }
00088 CloseHandle(Overlapped.hEvent);
00089
00090 return error_code;
00091 }
00092 #else
00093 int ADIOI_Set_lock(FDTYPE fd, int cmd, int type, ADIO_Offset offset, int whence,
00094 ADIO_Offset len)
00095 {
00096 int err, error_code, err_count = 0, sav_errno;
00097 struct flock lock;
00098
00099 if (len == 0) return MPI_SUCCESS;
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 #ifdef NEEDS_INT_CAST_WITH_FLOCK
00112 lock.l_type = type;
00113 lock.l_start = (int)offset;
00114 lock.l_whence = whence;
00115 lock.l_len = (int)len;
00116 #else
00117 lock.l_type = type;
00118 lock.l_whence = whence;
00119 lock.l_start = offset;
00120 lock.l_len = len;
00121 #endif
00122
00123 sav_errno = errno;
00124 errno = 0;
00125 do {
00126 err = fcntl(fd, cmd, &lock);
00127 #ifdef USE_DBG_LOGGING
00128
00129 {
00130 if (err && ((errno == EINTR) || (errno == EINPROGRESS)))
00131 {
00132 if((err_count < 5) || (err_count > 9995))
00133 {
00134 fprintf(stderr, "File locking failed in ADIOI_Set_lock(fd %#X,cmd %s/%#X,type %s/%#X,whence %#X) with return value %#X and errno %#X. Retry (%d).\n",
00135 fd,
00136 ((cmd == F_GETLK )? "F_GETLK" :
00137 ((cmd == F_SETLK )? "F_SETLK" :
00138 ((cmd == F_SETLKW )? "F_SETLKW" : "UNEXPECTED"))),
00139 cmd,
00140 ((type == F_RDLCK )? "F_RDLCK" :
00141 ((type == F_WRLCK )? "F_WRLCK" :
00142 ((type == F_UNLCK )? "F_UNLOCK" : "UNEXPECTED"))),
00143 type,
00144 whence, err, errno, err_count);
00145 perror("ADIOI_Set_lock:");
00146 fprintf(stderr,"ADIOI_Set_lock:offset %#llx, length %#llx\n",(unsigned long long)offset, (unsigned long long)len);
00147 }
00148 }
00149 }
00150 #endif
00151 } while (err && ((errno == EINTR) || ((errno == EINPROGRESS) && (++err_count < 10000))));
00152
00153 if (err && (errno != EBADF)) {
00154
00155
00156 FPRINTF(stderr, "File locking failed in ADIOI_Set_lock(fd %X,cmd %s/%X,type %s/%X,whence %X) with return value %X and errno %X.\n"
00157 "- If the file system is NFS, you need to use NFS version 3, ensure that the lockd daemon is running on all the machines, and mount the directory with the 'noac' option (no attribute caching).\n"
00158 "- If the file system is LUSTRE, ensure that the directory is mounted with the 'flock' option.\n",
00159 fd,
00160 ((cmd == F_GETLK )? "F_GETLK" :
00161 ((cmd == F_SETLK )? "F_SETLK" :
00162 ((cmd == F_SETLKW )? "F_SETLKW" : "UNEXPECTED"))),
00163 cmd,
00164 ((type == F_RDLCK )? "F_RDLCK" :
00165 ((type == F_WRLCK )? "F_WRLCK" :
00166 ((type == F_UNLCK )? "F_UNLOCK" : "UNEXPECTED"))),
00167 type,
00168 whence, err, errno);
00169 perror("ADIOI_Set_lock:");
00170 FPRINTF(stderr,"ADIOI_Set_lock:offset %llu, length %llu\n",(unsigned long long)offset, (unsigned long long)len);
00171 MPI_Abort(MPI_COMM_WORLD, 1);
00172 }
00173
00174 if(!err)
00175 errno = sav_errno;
00176
00177 error_code = (err == 0) ? MPI_SUCCESS : MPI_ERR_UNKNOWN;
00178 return error_code;
00179 }
00180 #endif
00181
00182 #if (defined(ROMIO_HFS) || defined(ROMIO_XFS))
00183 int ADIOI_Set_lock64(FDTYPE fd, int cmd, int type, ADIO_Offset offset,
00184 int whence,
00185 ADIO_Offset len)
00186 {
00187 int err, error_code;
00188 struct flock64 lock;
00189
00190 if (len == 0) return MPI_SUCCESS;
00191
00192 lock.l_type = type;
00193 lock.l_start = offset;
00194 lock.l_whence = whence;
00195 lock.l_len = len;
00196
00197 do {
00198 err = fcntl(fd, cmd, &lock);
00199 } while (err && (errno == EINTR));
00200
00201 if (err && (errno != EBADF)) {
00202 FPRINTF(stderr, "File locking failed in ADIOI_Set_lock64(fd %X,cmd %s/%X,type %s/%X,whence %X) with return value %X and errno %X.\n"
00203 "If the file system is NFS, you need to use NFS version 3, ensure that the lockd daemon is running on all the machines, and mount the directory with the 'noac' option (no attribute caching).\n",
00204 fd,
00205 ((cmd == F_GETLK )? "F_GETLK" :
00206 ((cmd == F_SETLK )? "F_SETLK" :
00207 ((cmd == F_SETLKW )? "F_SETLKW" :
00208 ((cmd == F_GETLK64 )? "F_GETLK64" :
00209 ((cmd == F_SETLK64 )? "F_SETLK64" :
00210 ((cmd == F_SETLKW64)? "F_SETLKW64" : "UNEXPECTED")))))),
00211 cmd,
00212 ((type == F_RDLCK )? "F_RDLCK" :
00213 ((type == F_WRLCK )? "F_WRLCK" :
00214 ((type == F_UNLCK )? "F_UNLOCK" : "UNEXPECTED"))),
00215 type,
00216 whence, err, errno);
00217 perror("ADIOI_Set_lock64:");
00218 FPRINTF(stderr,"ADIOI_Set_lock:offset %llu, length %llu\n",(unsigned long long)offset, (unsigned long long)len);
00219 MPI_Abort(MPI_COMM_WORLD, 1);
00220 }
00221
00222 error_code = (err == 0) ? MPI_SUCCESS : MPI_ERR_UNKNOWN;
00223 return error_code;
00224 }
00225 #endif