f9bde77d2060f0a1d5bb9620a7f3d6327f19088f
[reactos.git] / reactos / drivers / filesystems / fastfat / cleanup.c
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 */
8
9 /* INCLUDES *****************************************************************/
10
11 #include "vfat.h"
12
13 #define NDEBUG
14 #include <debug.h>
15
16 /* FUNCTIONS ****************************************************************/
17
18 /*
19 * FUNCTION: Cleans up after a file has been closed.
20 */
21 static
22 NTSTATUS
23 VfatCleanupFile(
24 PVFAT_IRP_CONTEXT IrpContext)
25 {
26 PVFATFCB pFcb;
27 PDEVICE_EXTENSION DeviceExt = IrpContext->DeviceExt;
28 PFILE_OBJECT FileObject = IrpContext->FileObject;
29
30 DPRINT("VfatCleanupFile(DeviceExt %p, FileObject %p)\n",
31 IrpContext->DeviceExt, FileObject);
32
33 /* FIXME: handle file/directory deletion here */
34 pFcb = (PVFATFCB)FileObject->FsContext;
35 if (!pFcb)
36 return STATUS_SUCCESS;
37
38 if (pFcb->Flags & FCB_IS_VOLUME)
39 {
40 pFcb->OpenHandleCount--;
41 DeviceExt->OpenHandleCount--;
42
43 if (pFcb->OpenHandleCount != 0)
44 {
45 IoRemoveShareAccess(FileObject, &pFcb->FCBShareAccess);
46 }
47 }
48 else
49 {
50 if(!ExAcquireResourceExclusiveLite(&pFcb->MainResource,
51 (BOOLEAN)(IrpContext->Flags & IRPCONTEXT_CANWAIT)))
52 {
53 return STATUS_PENDING;
54 }
55 if(!ExAcquireResourceExclusiveLite(&pFcb->PagingIoResource,
56 (BOOLEAN)(IrpContext->Flags & IRPCONTEXT_CANWAIT)))
57 {
58 ExReleaseResourceLite(&pFcb->MainResource);
59 return STATUS_PENDING;
60 }
61
62 /* Notify about the cleanup */
63 FsRtlNotifyCleanup(IrpContext->DeviceExt->NotifySync,
64 &(IrpContext->DeviceExt->NotifyList),
65 FileObject->FsContext2);
66
67 pFcb->OpenHandleCount--;
68 DeviceExt->OpenHandleCount--;
69
70 if (!(*pFcb->Attributes & FILE_ATTRIBUTE_DIRECTORY) &&
71 FsRtlAreThereCurrentFileLocks(&pFcb->FileLock))
72 {
73 /* remove all locks this process have on this file */
74 FsRtlFastUnlockAll(&pFcb->FileLock,
75 FileObject,
76 IoGetRequestorProcess(IrpContext->Irp),
77 NULL);
78 }
79
80 if (pFcb->Flags & FCB_IS_DIRTY)
81 {
82 VfatUpdateEntry (pFcb);
83 }
84
85 if (pFcb->Flags & FCB_DELETE_PENDING &&
86 pFcb->OpenHandleCount == 0)
87 {
88 PFILE_OBJECT tmpFileObject;
89 tmpFileObject = pFcb->FileObject;
90 if (tmpFileObject != NULL)
91 {
92 pFcb->FileObject = NULL;
93 CcUninitializeCacheMap(tmpFileObject, NULL, NULL);
94 ObDereferenceObject(tmpFileObject);
95 }
96
97 CcPurgeCacheSection(FileObject->SectionObjectPointer, NULL, 0, FALSE);
98 }
99
100 /* Uninitialize the cache (should be done even if caching was never initialized) */
101 CcUninitializeCacheMap(FileObject, &pFcb->RFCB.FileSize, NULL);
102
103 if (pFcb->OpenHandleCount != 0)
104 {
105 IoRemoveShareAccess(FileObject, &pFcb->FCBShareAccess);
106 }
107
108 FileObject->Flags |= FO_CLEANUP_COMPLETE;
109
110 ExReleaseResourceLite(&pFcb->PagingIoResource);
111 ExReleaseResourceLite(&pFcb->MainResource);
112 }
113
114 return STATUS_SUCCESS;
115 }
116
117 /*
118 * FUNCTION: Cleans up after a file has been closed.
119 */
120 NTSTATUS
121 VfatCleanup(
122 PVFAT_IRP_CONTEXT IrpContext)
123 {
124 NTSTATUS Status;
125
126 DPRINT("VfatCleanup(DeviceObject %p, Irp %p)\n", IrpContext->DeviceObject, IrpContext->Irp);
127
128 if (IrpContext->DeviceObject == VfatGlobalData->DeviceObject)
129 {
130 Status = STATUS_SUCCESS;
131 goto ByeBye;
132 }
133
134 if (!ExAcquireResourceExclusiveLite(&IrpContext->DeviceExt->DirResource,
135 (BOOLEAN)(IrpContext->Flags & IRPCONTEXT_CANWAIT)))
136 {
137 return VfatQueueRequest(IrpContext);
138 }
139
140 Status = VfatCleanupFile(IrpContext);
141
142 ExReleaseResourceLite(&IrpContext->DeviceExt->DirResource);
143
144 if (Status == STATUS_PENDING)
145 {
146 return VfatQueueRequest(IrpContext);
147 }
148
149 ByeBye:
150 IrpContext->Irp->IoStatus.Status = Status;
151 IrpContext->Irp->IoStatus.Information = 0;
152
153 IoCompleteRequest(IrpContext->Irp, IO_NO_INCREMENT);
154 VfatFreeIrpContext(IrpContext);
155 return Status;
156 }
157
158 /* EOF */