ReactOS  0.4.13-dev-241-g63286c6
cleanup.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS kernel
4  * FILE: drivers/fs/vfat/cleanup.c
5  * PURPOSE: VFAT Filesystem
6  * PROGRAMMER: Jason Filby (jasonfilby@yahoo.com)
7  * Pierre Schweitzer (pierre@reactos.org)
8  */
9 
10 /* INCLUDES *****************************************************************/
11 
12 #include "vfat.h"
13 
14 #define NDEBUG
15 #include <debug.h>
16 
17 /* FUNCTIONS ****************************************************************/
18 
19 /*
20  * FUNCTION: Cleans up after a file has been closed.
21  */
22 static
25  PVFAT_IRP_CONTEXT IrpContext)
26 {
27  PVFATFCB pFcb;
28  PVFATCCB pCcb;
29  BOOLEAN IsVolume;
30  PDEVICE_EXTENSION DeviceExt = IrpContext->DeviceExt;
31  PFILE_OBJECT FileObject = IrpContext->FileObject;
32 
33  DPRINT("VfatCleanupFile(DeviceExt %p, FileObject %p)\n",
34  IrpContext->DeviceExt, FileObject);
35 
36  /* FIXME: handle file/directory deletion here */
37  pFcb = (PVFATFCB)FileObject->FsContext;
38  if (!pFcb)
39  return STATUS_SUCCESS;
40 
41  IsVolume = BooleanFlagOn(pFcb->Flags, FCB_IS_VOLUME);
42  if (IsVolume)
43  {
44  pFcb->OpenHandleCount--;
45  DeviceExt->OpenHandleCount--;
46 
47  if (pFcb->OpenHandleCount != 0)
48  {
50  }
51  }
52  else
53  {
56 
57  pCcb = FileObject->FsContext2;
59  {
60  pFcb->Flags |= FCB_DELETE_PENDING;
61  }
62 
63  /* Notify about the cleanup */
64  FsRtlNotifyCleanup(IrpContext->DeviceExt->NotifySync,
65  &(IrpContext->DeviceExt->NotifyList),
66  FileObject->FsContext2);
67 
68  pFcb->OpenHandleCount--;
69  DeviceExt->OpenHandleCount--;
70 
71  if (!vfatFCBIsDirectory(pFcb) &&
73  {
74  /* remove all locks this process have on this file */
76  FileObject,
77  IoGetRequestorProcess(IrpContext->Irp),
78  NULL);
79  }
80 
81  if (BooleanFlagOn(pFcb->Flags, FCB_IS_DIRTY))
82  {
83  VfatUpdateEntry (DeviceExt, pFcb);
84  }
85 
87  pFcb->OpenHandleCount == 0)
88  {
89  if (vfatFCBIsDirectory(pFcb) &&
90  !VfatIsDirectoryEmpty(DeviceExt, pFcb))
91  {
92  pFcb->Flags &= ~FCB_DELETE_PENDING;
93  }
94  else
95  {
96  PFILE_OBJECT tmpFileObject;
97  tmpFileObject = pFcb->FileObject;
98  if (tmpFileObject != NULL)
99  {
100  pFcb->FileObject = NULL;
101  CcUninitializeCacheMap(tmpFileObject, NULL, NULL);
103  ObDereferenceObject(tmpFileObject);
104  }
105 
106  pFcb->RFCB.ValidDataLength.QuadPart = 0;
107  pFcb->RFCB.FileSize.QuadPart = 0;
108  pFcb->RFCB.AllocationSize.QuadPart = 0;
109  }
110  }
111 
112  /* Uninitialize the cache (should be done even if caching was never initialized) */
114 
115  if (BooleanFlagOn(pFcb->Flags, FCB_DELETE_PENDING) &&
116  pFcb->OpenHandleCount == 0)
117  {
118  VfatDelEntry(DeviceExt, pFcb, NULL);
119 
120  vfatReportChange(DeviceExt,
121  pFcb,
122  (vfatFCBIsDirectory(pFcb) ?
125  }
126 
127  if (pFcb->OpenHandleCount != 0)
128  {
130  }
131 /* FIXME: causes FS corruption and breaks selfhosting/testbots and so on */
132 #if 0
133  /* If that's the last open handle we just closed, try to see whether
134  * we can delay close operation
135  */
138  {
139  /* This is only allowed if that's a directory with no open files
140  * OR if it's a file with no section opened
141  */
142  if ((vfatFCBIsDirectory(pFcb) && IsListEmpty(&pFcb->ParentListHead)) ||
143  (!vfatFCBIsDirectory(pFcb) && FileObject->SectionObjectPointer->DataSectionObject == NULL &&
144  FileObject->SectionObjectPointer->ImageSectionObject == NULL))
145  {
146  DPRINT("Delaying close of: %wZ\n", &pFcb->PathNameU);
148  }
149  }
150 #endif
151 
153 #ifdef KDBG
154  pFcb->Flags |= FCB_CLEANED_UP;
155 #endif
156 
159  }
160 
161 #ifdef ENABLE_SWAPOUT
162  if (IsVolume && BooleanFlagOn(DeviceExt->Flags, VCB_DISMOUNT_PENDING))
163  {
164  VfatCheckForDismount(DeviceExt, TRUE);
165  }
166 #endif
167 
168  return STATUS_SUCCESS;
169 }
170 
171 /*
172  * FUNCTION: Cleans up after a file has been closed.
173  */
174 NTSTATUS
176  PVFAT_IRP_CONTEXT IrpContext)
177 {
179 
180  DPRINT("VfatCleanup(DeviceObject %p, Irp %p)\n", IrpContext->DeviceObject, IrpContext->Irp);
181 
182  if (IrpContext->DeviceObject == VfatGlobalData->DeviceObject)
183  {
184  IrpContext->Irp->IoStatus.Information = 0;
185  return STATUS_SUCCESS;
186  }
187 
188  ExAcquireResourceExclusiveLite(&IrpContext->DeviceExt->DirResource, TRUE);
189  Status = VfatCleanupFile(IrpContext);
190  ExReleaseResourceLite(&IrpContext->DeviceExt->DirResource);
191 
192  IrpContext->Irp->IoStatus.Information = 0;
193  return Status;
194 }
195 
196 /* EOF */
PEPROCESS NTAPI IoGetRequestorProcess(IN PIRP Irp)
Definition: irp.c:1782
#define TRUE
Definition: types.h:120
#define FCB_IS_DIRTY
Definition: vfat.h:432
#define CCB_DELETE_ON_CLOSE
Definition: ext2fs.h:1026
Definition: vfat.h:441
Definition: vfat.h:530
PDEVICE_EXTENSION DeviceExt
Definition: vfat.h:579
#define BooleanFlagOn(F, SF)
Definition: ext2fs.h:183
NTSTATUS VfatCleanup(PVFAT_IRP_CONTEXT IrpContext)
Definition: cleanup.c:175
static NTSTATUS VfatCleanupFile(PVFAT_IRP_CONTEXT IrpContext)
Definition: cleanup.c:24
LONG NTSTATUS
Definition: precomp.h:26
FORCEINLINE BOOLEAN vfatFCBIsDirectory(PVFATFCB FCB)
Definition: vfat.h:631
#define FILE_NOTIFY_CHANGE_FILE_NAME
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
PFILE_OBJECT FileObject
Definition: vfat.h:585
#define FILE_NOTIFY_CHANGE_DIR_NAME
_Must_inspect_result_ FORCEINLINE BOOLEAN IsListEmpty(_In_ const LIST_ENTRY *ListHead)
Definition: rtlfuncs.h:57
VOID NTAPI IoRemoveShareAccess(IN PFILE_OBJECT FileObject, IN PSHARE_ACCESS ShareAccess)
Definition: file.c:3477
#define FCB_DELETE_PENDING
Definition: ext2fs.h:879
VOID NTAPI FsRtlNotifyCleanup(IN PNOTIFY_SYNC NotifySync, IN PLIST_ENTRY NotifyList, IN PVOID FsContext)
Definition: notify.c:635
BOOLEAN NTAPI ExAcquireResourceExclusiveLite(IN PERESOURCE Resource, IN BOOLEAN Wait)
Definition: resource.c:770
ULONG Flags
Definition: vfat.h:533
PFILE_OBJECT FileObject
Definition: vfat.h:493
ULONG Flags
Definition: vfat.h:490
SHARE_ACCESS FCBShareAccess
Definition: vfat.h:502
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
#define FILE_ACTION_REMOVED
void DPRINT(...)
Definition: polytest.cpp:61
_Inout_ PFILE_OBJECT FileObject
Definition: cdprocs.h:593
#define FsRtlAreThereCurrentFileLocks(FL)
Definition: fsrtlfuncs.h:1584
PDEVICE_OBJECT DeviceObject
Definition: vfat.h:406
if(!(yy_init))
Definition: macro.lex.yy.c:714
#define FCB_IS_PAGE_FILE
Definition: vfat.h:430
VOID FASTCALL ExReleaseResourceLite(IN PERESOURCE Resource)
Definition: resource.c:1817
* PFILE_OBJECT
Definition: iotypes.h:1954
FSRTL_COMMON_FCB_HEADER RFCB
Definition: vfat.h:444
FILE_LOCK FileLock
Definition: vfat.h:514
#define FCB_IS_FAT
Definition: vfat.h:429
FORCEINLINE BOOLEAN VfatIsDirectoryEmpty(PDEVICE_EXTENSION DeviceExt, struct _VFATFCB *Fcb)
Definition: vfat.h:361
#define VCB_DISMOUNT_PENDING
Definition: ext2fs.h:782
LARGE_INTEGER AllocationSize
Definition: env_spec_w32.h:755
NTSTATUS NTAPI FsRtlFastUnlockAll(IN PFILE_LOCK FileLock, IN PFILE_OBJECT FileObject, IN PEPROCESS Process, IN PVOID Context OPTIONAL)
Definition: filelock.c:1026
BOOLEAN VfatCheckForDismount(IN PDEVICE_EXTENSION DeviceExt, IN BOOLEAN Force)
Definition: misc.c:417
ClearFlag(Dirent->Flags, DIRENT_FLAG_NOT_PERSISTENT)
struct _VFATFCB * PVFATFCB
Status
Definition: gdiplustypes.h:24
#define FCB_DELAYED_CLOSE
Definition: vfat.h:433
#define FCB_CACHE_INITIALIZED
Definition: vfat.h:427
PVFAT_GLOBAL_DATA VfatGlobalData
Definition: iface.c:40
#define FCB_IS_VOLUME
Definition: vfat.h:431
#define SetFlag(_F, _SF)
Definition: ext2fs.h:187
ERESOURCE PagingIoResource
Definition: vfat.h:447
FORCEINLINE VOID vfatReportChange(IN PDEVICE_EXTENSION DeviceExt, IN PVFATFCB Fcb, IN ULONG FilterMatch, IN ULONG Action)
Definition: vfat.h:652
BOOLEAN NTAPI CcUninitializeCacheMap(IN PFILE_OBJECT FileObject, IN OPTIONAL PLARGE_INTEGER TruncateSize, IN OPTIONAL PCACHE_UNINITIALIZE_EVENT UninitializeEvent)
Definition: fssup.c:284
FORCEINLINE NTSTATUS VfatDelEntry(PDEVICE_EXTENSION DeviceExt, struct _VFATFCB *Fcb, struct _VFAT_MOVE_CONTEXT *MoveContext)
Definition: vfat.h:382
#define FO_CLEANUP_COMPLETE
Definition: iotypes.h:1746
PDEVICE_OBJECT DeviceObject
Definition: vfat.h:578
ERESOURCE MainResource
Definition: vfat.h:446
UNICODE_STRING PathNameU
Definition: vfat.h:466
NTSTATUS VfatUpdateEntry(IN PDEVICE_EXTENSION DeviceExt, IN PVFATFCB pFcb)
Definition: dirwr.c:117
ULONG OpenHandleCount
Definition: vfat.h:505
LARGE_INTEGER ValidDataLength
Definition: env_spec_w32.h:757
return STATUS_SUCCESS
Definition: btrfs.c:2745
LIST_ENTRY ParentListHead
Definition: vfat.h:487
LONGLONG QuadPart
Definition: typedefs.h:112