811
812
ASSERT(file->lockToken == NULL);
812
813
ASSERT_ON_COMPILE(FILEIO_ERROR_LAST < 16); /* See comment in fileIO.h */
815
FileIOResolveLockBits(&access);
816
ASSERT((access & FILEIO_OPEN_LOCKED) == 0 &&
817
(access & FILEIO_OPEN_EXCLUSIVE_LOCK) == 0);
818
/* Only ESX implements mandatory locking */
819
ASSERT((access & FILEIO_OPEN_LOCK_MANDATORY) == 0 ||
820
File_SupportsMandatoryLock(pathName));
814
822
#if defined(__APPLE__)
815
823
if (access & FILEIO_OPEN_EXCLUSIVE_LOCK_MACOS) {
816
824
flags |= O_EXLOCK;
818
#elif !defined(__FreeBSD__) && !defined(sun)
826
#elif defined(__linux__)
827
if (((access & (FILEIO_OPEN_LOCK_MANDATORY |
828
FILEIO_OPEN_MULTIWRITER_LOCK)) != 0) &&
829
HostType_OSIsVMK()) {
830
/* These flags are only supported on vmkernel */
831
if ((access & FILEIO_OPEN_MULTIWRITER_LOCK) != 0) {
832
flags |= O_MULTIWRITER_LOCK;
833
} else if ((access & FILEIO_OPEN_LOCK_MANDATORY) != 0) {
834
flags |= O_EXCLUSIVE_LOCK;
820
* If FILEIO_OPEN_EXCLUSIVE_LOCK or FILEIO_OPEN_MULTIWRITER_LOCK or
821
* (FILEIO_OPEN_ACCESS_READ | FILEIO_OPEN_LOCKED) are passed, and we are
822
* on VMFS, then pass in special flags to get exclusive, multiwriter, or
823
* cross-host read-only mode. The first if statement is to avoid calling
824
* HostType_OSIsVMK() unless really necessary.
826
* If the above conditions are met FILEIO_OPEN_LOCKED, is filtered out --
827
* vmfs will be handling the locking, so there is no need to create
840
* Locking implementation note: this can be recursive. On ESX:
841
* FileIOCreateRetry("foo", ...ADVISORY...)
842
* -> FileIO_Lock("foo", ...ADVISORY...)
843
* -> FileLock_Lock("foo", ...ADVISORY...)
844
* -> FileIOCreateRetry("foo.lck", ...MANDATORY...)
845
* -> open("foo.lck", ...O_EXCLUSIVE_LOCK...)
830
if ((access & (FILEIO_OPEN_EXCLUSIVE_LOCK |
831
FILEIO_OPEN_MULTIWRITER_LOCK)) != 0 ||
832
(access & (FILEIO_OPEN_ACCESS_READ | FILEIO_OPEN_ACCESS_WRITE |
833
FILEIO_OPEN_LOCKED)) ==
834
(FILEIO_OPEN_ACCESS_READ | FILEIO_OPEN_LOCKED)) {
835
if (HostType_OSIsVMK()) {
836
access &= ~FILEIO_OPEN_LOCKED;
838
if ((access & FILEIO_OPEN_MULTIWRITER_LOCK) != 0) {
839
flags |= O_MULTIWRITER_LOCK;
841
flags |= O_EXCLUSIVE_LOCK;
847
848
FileIO_Init(file, pathName);
848
ret = FileIO_Lock(file, access);
849
if (!FileIO_IsSuccess(ret)) {
849
/* Mandatory file locks are only available at open() itself */
850
if ((access & FILEIO_OPEN_LOCK_ADVISORY) != 0) {
851
ret = FileIO_Lock(file, access);
852
if (!FileIO_IsSuccess(ret)) {
853
857
if ((access & (FILEIO_OPEN_ACCESS_READ | FILEIO_OPEN_ACCESS_WRITE)) ==
1877
1881
#endif /* defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) ||
1878
1882
defined(sun) */
1881
*----------------------------------------------------------------------
1888
* Size of file or -1.
1893
*----------------------------------------------------------------------
1897
FileIO_GetSize(const FileIODescriptor *fd) // IN:
1899
struct stat statBuf;
1903
return (fstat(fd->posix, &statBuf) == -1) ? -1 : statBuf.st_size;
1908
1886
*----------------------------------------------------------------------
1910
1888
* FileIO_GetAllocSize --
1912
* Get allocated size of file.
1890
* Get logcial and alloced size of a file.
1915
* Size of file or -1.
1917
1895
* Side effects:
1920
1898
*----------------------------------------------------------------------
1924
FileIO_GetAllocSize(const FileIODescriptor *fd) // IN
1902
FileIO_GetAllocSize(const FileIODescriptor *fd, // IN:
1903
uint64 *logicalBytes, // OUT:
1904
uint64 *allocedBytes) // OUT:
1906
struct stat statBuf;
1931
* If you don't like the magic number 512, yell at the people
1932
* who wrote sys/stat.h and tell them to add a #define for it.
1935
return (fstat(fd->posix, &s) == -1) ? -1 : s.st_blocks * 512;
1910
if (fstat(fd->posix, &statBuf) == -1) {
1911
return FileIOErrno2Result(errno);
1915
*logicalBytes = statBuf.st_size;
1920
* If you don't like the magic number 512, yell at the people
1921
* who wrote sys/stat.h and tell them to add a #define for it.
1923
*allocedBytes = statBuf.st_blocks * 512;
1926
return FILEIO_SUCCESS;
2010
2006
*----------------------------------------------------------------------
2014
FileIO_GetSizeByPath(ConstUnicode pathName) // IN:
2010
FileIO_GetAllocSizeByPath(ConstUnicode pathName, // IN:
2011
uint64 *logicalBytes, // OUT:
2012
uint64 *allocedBytes) // OUT:
2017
struct stat statbuf;
2019
err = Posix_Stat(pathName, &statbuf);
2021
return (err == 0) ? statbuf.st_size : -1;
2014
struct stat statBuf;
2016
if (Posix_Stat(pathName, &statBuf) == -1) {
2017
return FileIOErrno2Result(errno);
2021
*logicalBytes = statBuf.st_size;
2025
#if __linux__ && defined(N_PLAT_NLM)
2026
/* Netware doesn't have st_blocks. Just fall back to GetSize. */
2027
*allocedBytes = statBuf.st_size;
2030
* If you don't like the magic number 512, yell at the people
2031
* who wrote sys/stat.h and tell them to add a #define for it.
2033
*allocedBytes = statBuf.st_blocks * 512;
2037
return FILEIO_SUCCESS;