ReactOS  0.4.14-dev-50-g13bb5e2
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  * Returns whether the device was deleted
22  */
23 static
24 BOOLEAN
26  PVFAT_IRP_CONTEXT IrpContext)
27 {
28  PVFATFCB pFcb;
29  PVFATCCB pCcb;
30  BOOLEAN IsVolume;
31  PDEVICE_EXTENSION DeviceExt = IrpContext->DeviceExt;
32  PFILE_OBJECT FileObject = IrpContext->FileObject;
34 
35  DPRINT("VfatCleanupFile(DeviceExt %p, FileObject %p)\n",
36  IrpContext->DeviceExt, FileObject);
37 
38  /* FIXME: handle file/directory deletion here */
39  pFcb = (PVFATFCB)FileObject->FsContext;
40  if (!pFcb)
41  return FALSE;
42 
43  IsVolume = BooleanFlagOn(pFcb->Flags, FCB_IS_VOLUME);
44  if (IsVolume)
45  {
46  pFcb->OpenHandleCount--;
47  DeviceExt->OpenHandleCount--;
48 
49  if (pFcb->OpenHandleCount != 0)
50  {
52  }
53  }
54  else
55  {
58 
59  pCcb = FileObject->FsContext2;
61  {
62  pFcb->Flags |= FCB_DELETE_PENDING;
63  }
64 
65  /* Notify about the cleanup */
66  FsRtlNotifyCleanup(IrpContext->DeviceExt->NotifySync,
67  &(IrpContext->DeviceExt->NotifyList),
68  FileObject->FsContext2);
69 
70  pFcb->OpenHandleCount--;
71  DeviceExt->OpenHandleCount--;
72 
73  if (!vfatFCBIsDirectory(pFcb) &&
75  {
76  /* remove all locks this process have on this file */
78  FileObject,
79  IoGetRequestorProcess(IrpContext->Irp),
80  NULL);
81  }
82 
83  if (BooleanFlagOn(pFcb->Flags, FCB_IS_DIRTY))
84  {
85  VfatUpdateEntry (DeviceExt, pFcb);
86  }
87 
89  pFcb->OpenHandleCount == 0)
90  {
91  if (vfatFCBIsDirectory(pFcb) &&
92  !VfatIsDirectoryEmpty(DeviceExt, pFcb))
93  {
94  pFcb->Flags &= ~FCB_DELETE_PENDING;
95  }
96  else
97  {
98  PFILE_OBJECT tmpFileObject;
99  tmpFileObject = pFcb->FileObject;
100  if (tmpFileObject != NULL)
101  {
102  pFcb->FileObject = NULL;
103  CcUninitializeCacheMap(tmpFileObject, NULL, NULL);
105  ObDereferenceObject(tmpFileObject);
106  }
107 
108  pFcb->RFCB.ValidDataLength.QuadPart = 0;
109  pFcb->RFCB.FileSize.QuadPart = 0;
110  pFcb->RFCB.AllocationSize.QuadPart = 0;
111  }
112  }
113 
114  /* Uninitialize the cache (should be done even if caching was never initialized) */
116 
117  if (BooleanFlagOn(pFcb->Flags, FCB_DELETE_PENDING) &&
118  pFcb->OpenHandleCount == 0)
119  {
120  VfatDelEntry(DeviceExt, pFcb, NULL);
121 
122  vfatReportChange(DeviceExt,
123  pFcb,
124  (vfatFCBIsDirectory(pFcb) ?
127  }
128 
129  if (pFcb->OpenHandleCount != 0)
130  {
132  }
133 /* FIXME: causes FS corruption and breaks selfhosting/testbots and so on */
134 #if 0
135  /* If that's the last open handle we just closed, try to see whether
136  * we can delay close operation
137  */
140  {
141  /* This is only allowed if that's a directory with no open files
142  * OR if it's a file with no section opened
143  */
144  if ((vfatFCBIsDirectory(pFcb) && IsListEmpty(&pFcb->ParentListHead)) ||
145  (!vfatFCBIsDirectory(pFcb) && FileObject->SectionObjectPointer->DataSectionObject == NULL &&
146  FileObject->SectionObjectPointer->ImageSectionObject == NULL))
147  {
148  DPRINT("Delaying close of: %wZ\n", &pFcb->PathNameU);
150  }
151  }
152 #endif
153 
155 #ifdef KDBG
156  pFcb->Flags |= FCB_CLEANED_UP;
157 #endif
158 
161  }
162 
163 #ifdef ENABLE_SWAPOUT
164  if (IsVolume && BooleanFlagOn(DeviceExt->Flags, VCB_DISMOUNT_PENDING))
165  {
166  Deleted = VfatCheckForDismount(DeviceExt, TRUE);
167  }
168 #endif
169 
170  return Deleted;
171 }
172 
173 /*
174  * FUNCTION: Cleans up after a file has been closed.
175  */
176 NTSTATUS
178  PVFAT_IRP_CONTEXT IrpContext)
179 {
181 
182  DPRINT("VfatCleanup(DeviceObject %p, Irp %p)\n", IrpContext->DeviceObject, IrpContext->Irp);
183 
184  if (IrpContext->DeviceObject == VfatGlobalData->DeviceObject)
185  {
186  IrpContext->Irp->IoStatus.Information = 0;
187  return STATUS_SUCCESS;
188  }
189 
190  ExAcquireResourceExclusiveLite(&IrpContext->DeviceExt->DirResource, TRUE);
191  Deleted = VfatCleanupFile(IrpContext);
192  if (!Deleted) ExReleaseResourceLite(&IrpContext->DeviceExt->DirResource);
193 
194  IrpContext->Irp->IoStatus.Information = 0;
195  return STATUS_SUCCESS;
196 }
197 
198 /* 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
static BOOLEAN VfatCleanupFile(PVFAT_IRP_CONTEXT IrpContext)
Definition: cleanup.c:25
#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:177
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:1955
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
#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:1747
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:2966
LIST_ENTRY ParentListHead
Definition: vfat.h:487
LONGLONG QuadPart
Definition: typedefs.h:112