sys,dll: Cleanup/TruncateOnClose overhaul

This commit is contained in:
Bill Zissimopoulos
2016-12-21 16:09:24 -08:00
parent 8d6314e2db
commit c897ddd864
13 changed files with 203 additions and 69 deletions

View File

@ -627,9 +627,8 @@ static NTSTATUS GetReparsePointByName(
PWSTR FileName, BOOLEAN IsDirectory, PVOID Buffer, PSIZE_T PSize);
#endif
static NTSTATUS SetFileSize(FSP_FILE_SYSTEM *FileSystem,
PVOID FileNode0, UINT64 NewSize, BOOLEAN SetAllocationSize,
FSP_FSCTL_FILE_INFO *FileInfo);
static NTSTATUS SetFileSizeInternal(FSP_FILE_SYSTEM *FileSystem,
PVOID FileNode0, UINT64 NewSize, BOOLEAN SetAllocationSize);
static NTSTATUS GetVolumeInfo(FSP_FILE_SYSTEM *FileSystem,
FSP_FSCTL_VOLUME_INFO *VolumeInfo)
@ -888,21 +887,6 @@ static NTSTATUS Open(FSP_FILE_SYSTEM *FileSystem,
return Result;
}
/*
* NTFS and FastFat do this at Cleanup time, but we are going to cheat.
*
* To properly implement this we should maintain some state of whether
* we modified the file or not. Alternatively we could have the driver
* report to us at Cleanup time whether the file was modified. [The
* FSD does maintain the FO_FILE_MODIFIED bit, but does not send it
* to us.]
*
* TBD.
*/
if (0 == (FileNode->FileInfo.FileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
(GrantedAccess & (FILE_WRITE_DATA | FILE_APPEND_DATA)))
FileNode->FileInfo.FileAttributes |= FILE_ATTRIBUTE_ARCHIVE;
MemfsFileNodeReference(FileNode);
*PFileNode = FileNode;
MemfsFileNodeGetFileInfo(FileNode, FileInfo);
@ -944,7 +928,7 @@ static NTSTATUS Overwrite(FSP_FILE_SYSTEM *FileSystem,
MemfsFileNodeMapEnumerateFree(&Context);
#endif
Result = SetFileSize(FileSystem, FileNode, AllocationSize, TRUE, FileInfo);
Result = SetFileSizeInternal(FileSystem, FileNode, AllocationSize, TRUE);
if (!NT_SUCCESS(Result))
return Result;
@ -954,8 +938,9 @@ static NTSTATUS Overwrite(FSP_FILE_SYSTEM *FileSystem,
FileNode->FileInfo.FileAttributes |= FileAttributes | FILE_ATTRIBUTE_ARCHIVE;
FileNode->FileInfo.FileSize = 0;
FileNode->FileInfo.LastAccessTime =
FileNode->FileInfo.LastWriteTime =
FileNode->FileInfo.LastAccessTime = MemfsGetSystemTime();
FileNode->FileInfo.ChangeTime = MemfsGetSystemTime();
MemfsFileNodeGetFileInfo(FileNode, FileInfo);
@ -963,14 +948,41 @@ static NTSTATUS Overwrite(FSP_FILE_SYSTEM *FileSystem,
}
static VOID Cleanup(FSP_FILE_SYSTEM *FileSystem,
PVOID FileNode0, PWSTR FileName, BOOLEAN Delete)
PVOID FileNode0, PWSTR FileName, ULONG Flags)
{
MEMFS *Memfs = (MEMFS *)FileSystem->UserContext;
MEMFS_FILE_NODE *FileNode = (MEMFS_FILE_NODE *)FileNode0;
assert(Delete); /* the new FSP_FSCTL_VOLUME_PARAMS::PostCleanupOnDeleteOnly ensures this */
assert(0 != Flags); /* FSP_FSCTL_VOLUME_PARAMS::PostCleanupWhenModifiedOnly ensures this */
if (Delete && !MemfsFileNodeMapHasChild(Memfs->FileNodeMap, FileNode))
if (Flags & FspCleanupSetArchiveBit)
{
if (0 == (FileNode->FileInfo.FileAttributes & FILE_ATTRIBUTE_DIRECTORY))
FileNode->FileInfo.FileAttributes |= FILE_ATTRIBUTE_ARCHIVE;
}
if (Flags & (FspCleanupSetLastAccessTime | FspCleanupSetLastWriteTime | FspCleanupSetChangeTime))
{
UINT64 SystemTime = MemfsGetSystemTime();
if (Flags & FspCleanupSetLastAccessTime)
FileNode->FileInfo.LastAccessTime = SystemTime;
if (Flags & FspCleanupSetLastWriteTime)
FileNode->FileInfo.LastWriteTime = SystemTime;
if (Flags & FspCleanupSetChangeTime)
FileNode->FileInfo.ChangeTime = SystemTime;
}
if (Flags & FspCleanupSetAllocationSize)
{
UINT64 AllocationUnit = MEMFS_SECTOR_SIZE * MEMFS_SECTORS_PER_ALLOCATION_UNIT;
UINT64 AllocationSize = (FileNode->FileInfo.FileSize + AllocationUnit - 1) /
AllocationUnit * AllocationUnit;
SetFileSizeInternal(FileSystem, FileNode, AllocationSize, TRUE);
}
if ((Flags & FspCleanupDelete) && !MemfsFileNodeMapHasChild(Memfs->FileNodeMap, FileNode))
{
#if defined(MEMFS_NAMED_STREAMS)
MEMFS_FILE_NODE_MAP_ENUM_CONTEXT Context = { FALSE };
@ -1062,7 +1074,7 @@ static NTSTATUS Write(FSP_FILE_SYSTEM *FileSystem,
EndOffset = Offset + Length;
if (EndOffset > FileNode->FileInfo.FileSize)
{
Result = SetFileSize(FileSystem, FileNode, EndOffset, FALSE, FileInfo);
Result = SetFileSizeInternal(FileSystem, FileNode, EndOffset, FALSE);
if (!NT_SUCCESS(Result))
return Result;
}
@ -1077,9 +1089,19 @@ static NTSTATUS Write(FSP_FILE_SYSTEM *FileSystem,
}
NTSTATUS Flush(FSP_FILE_SYSTEM *FileSystem,
PVOID FileNode)
PVOID FileNode0)
{
/* nothing to do, since we do not cache anything */
MEMFS_FILE_NODE *FileNode = (MEMFS_FILE_NODE *)FileNode0;
/* nothing to flush, since we do not cache anything */
if (0 != FileNode)
{
FileNode->FileInfo.LastAccessTime =
FileNode->FileInfo.LastWriteTime =
FileNode->FileInfo.ChangeTime = MemfsGetSystemTime();
}
return STATUS_SUCCESS;
}
@ -1120,9 +1142,8 @@ static NTSTATUS SetBasicInfo(FSP_FILE_SYSTEM *FileSystem,
return STATUS_SUCCESS;
}
static NTSTATUS SetFileSize(FSP_FILE_SYSTEM *FileSystem,
PVOID FileNode0, UINT64 NewSize, BOOLEAN SetAllocationSize,
FSP_FSCTL_FILE_INFO *FileInfo)
static NTSTATUS SetFileSizeInternal(FSP_FILE_SYSTEM *FileSystem,
PVOID FileNode0, UINT64 NewSize, BOOLEAN SetAllocationSize)
{
MEMFS *Memfs = (MEMFS *)FileSystem->UserContext;
MEMFS_FILE_NODE *FileNode = (MEMFS_FILE_NODE *)FileNode0;
@ -1154,8 +1175,7 @@ static NTSTATUS SetFileSize(FSP_FILE_SYSTEM *FileSystem,
UINT64 AllocationUnit = MEMFS_SECTOR_SIZE * MEMFS_SECTORS_PER_ALLOCATION_UNIT;
UINT64 AllocationSize = (NewSize + AllocationUnit - 1) / AllocationUnit * AllocationUnit;
NTSTATUS Result = SetFileSize(FileSystem, FileNode, AllocationSize, TRUE,
FileInfo);
NTSTATUS Result = SetFileSizeInternal(FileSystem, FileNode, AllocationSize, TRUE);
if (!NT_SUCCESS(Result))
return Result;
}
@ -1167,6 +1187,24 @@ static NTSTATUS SetFileSize(FSP_FILE_SYSTEM *FileSystem,
}
}
return STATUS_SUCCESS;
}
static NTSTATUS SetFileSize(FSP_FILE_SYSTEM *FileSystem,
PVOID FileNode0, UINT64 NewSize, BOOLEAN SetAllocationSize,
FSP_FSCTL_FILE_INFO *FileInfo)
{
MEMFS_FILE_NODE *FileNode = (MEMFS_FILE_NODE *)FileNode0;
NTSTATUS Result;
Result = SetFileSizeInternal(FileSystem, FileNode0, NewSize, SetAllocationSize);
if (!NT_SUCCESS(Result))
return Result;
FileNode->FileInfo.LastAccessTime =
FileNode->FileInfo.LastWriteTime =
FileNode->FileInfo.ChangeTime = MemfsGetSystemTime();
MemfsFileNodeGetFileInfo(FileNode, FileInfo);
return STATUS_SUCCESS;
@ -1319,6 +1357,8 @@ static NTSTATUS SetSecurity(FSP_FILE_SYSTEM *FileSystem,
FileNode->FileSecuritySize = FileSecuritySize;
FileNode->FileSecurity = FileSecurity;
FileNode->FileInfo.ChangeTime = MemfsGetSystemTime();
return STATUS_SUCCESS;
}
@ -1523,6 +1563,8 @@ static NTSTATUS SetReparsePoint(FSP_FILE_SYSTEM *FileSystem,
FileNode->ReparseData = ReparseData;
memcpy(FileNode->ReparseData, Buffer, Size);
FileNode->FileInfo.ChangeTime = MemfsGetSystemTime();
return STATUS_SUCCESS;
}
@ -1556,6 +1598,8 @@ static NTSTATUS DeleteReparsePoint(FSP_FILE_SYSTEM *FileSystem,
FileNode->ReparseDataSize = 0;
FileNode->ReparseData = 0;
FileNode->FileInfo.ChangeTime = MemfsGetSystemTime();
return STATUS_SUCCESS;
}
#endif
@ -1737,7 +1781,7 @@ NTSTATUS MemfsCreateFunnel(
#if defined(MEMFS_NAMED_STREAMS)
VolumeParams.NamedStreams = 1;
#endif
VolumeParams.PostCleanupOnDeleteOnly = 1;
VolumeParams.PostCleanupWhenModifiedOnly = 1;
if (0 != VolumePrefix)
wcscpy_s(VolumeParams.Prefix, sizeof VolumeParams.Prefix / sizeof(WCHAR), VolumePrefix);
wcscpy_s(VolumeParams.FileSystemName, sizeof VolumeParams.FileSystemName / sizeof(WCHAR),

View File

@ -288,9 +288,9 @@ void create_related_test(void)
create_related_dotest(MemfsNet, L"\\\\memfs\\share");
}
void create_allocation_dotest(ULONG Flags, PWSTR Prefix)
void create_allocation_dotest(ULONG Flags, PWSTR Prefix, ULONG FileInfoTimeout)
{
void *memfs = memfs_start(Flags);
void *memfs = memfs_start_ex(Flags, FileInfoTimeout);
HANDLE DirHandle, FileHandle, FileHandle2;
NTSTATUS Result;
@ -346,10 +346,27 @@ void create_allocation_dotest(ULONG Flags, PWSTR Prefix)
ASSERT(Success);
ASSERT(65536 == StandardInfo.AllocationSize.QuadPart);
CloseHandle(FileHandle2);
AllocationSize.QuadPart = 0;
UnicodePath.Length = (USHORT)wcslen(UnicodePathBuf) * sizeof(WCHAR);
UnicodePath.MaximumLength = sizeof UnicodePathBuf;
UnicodePath.Buffer = UnicodePathBuf;
InitializeObjectAttributes(&Obja, &UnicodePath, 0, DirHandle, 0);
Result = NtCreateFile(&FileHandle2,
FILE_READ_ATTRIBUTES, &Obja, &Iosb,
&AllocationSize, FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
FILE_OPEN, 0, 0, 0);
ASSERT(STATUS_SUCCESS == Result);
Success = GetFileInformationByHandleEx(FileHandle2, FileStandardInfo, &StandardInfo, sizeof StandardInfo);
ASSERT(Success);
ASSERT(65536 == StandardInfo.AllocationSize.QuadPart);
CloseHandle(FileHandle2);
CloseHandle(FileHandle);
#if 0
AllocationSize.QuadPart = 0;
UnicodePath.Length = (USHORT)wcslen(UnicodePathBuf) * sizeof(WCHAR);
UnicodePath.MaximumLength = sizeof UnicodePathBuf;
@ -366,7 +383,6 @@ void create_allocation_dotest(ULONG Flags, PWSTR Prefix)
ASSERT(0 == StandardInfo.AllocationSize.QuadPart);
CloseHandle(FileHandle);
#endif
StringCbPrintfW(FilePath, sizeof FilePath, L"%s%s\\dir1\\file2",
Prefix ? L"" : L"\\\\?\\GLOBALROOT", Prefix ? Prefix : memfs_volumename(memfs));
@ -388,12 +404,20 @@ void create_allocation_test(void)
{
WCHAR DirBuf[MAX_PATH];
GetTestDirectory(DirBuf);
create_allocation_dotest(-1, DirBuf);
create_allocation_dotest(-1, DirBuf, 0);
}
if (WinFspDiskTests)
create_allocation_dotest(MemfsDisk, 0);
{
create_allocation_dotest(MemfsDisk, 0, 0);
create_allocation_dotest(MemfsDisk, 0, 1000);
create_allocation_dotest(MemfsDisk, 0, INFINITE);
}
if (WinFspNetTests)
create_allocation_dotest(MemfsNet, L"\\\\memfs\\share");
{
create_allocation_dotest(MemfsNet, L"\\\\memfs\\share", 0);
create_allocation_dotest(MemfsNet, L"\\\\memfs\\share", 1000);
create_allocation_dotest(MemfsNet, L"\\\\memfs\\share", INFINITE);
}
}
void create_sd_dotest(ULONG Flags, PWSTR Prefix)