[NTFS]
[reactos.git] / reactos / drivers / filesystems / ntfs / cleanup.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2016 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 * COPYRIGHT: See COPYING in the top level directory
20 * PROJECT: ReactOS kernel
21 * FILE: drivers/filesystem/ntfs/cleanup.c
22 * PURPOSE: NTFS filesystem driver
23 * PROGRAMMER: Pierre Schweitzer (pierre@reactos.org)
24 * UPDATE HISTORY:
25 */
26
27 /* INCLUDES *****************************************************************/
28
29 #include "ntfs.h"
30
31 #define NDEBUG
32 #include <debug.h>
33
34 /* FUNCTIONS ****************************************************************/
35
36 /*
37 * FUNCTION: Cleans up a file
38 */
39 NTSTATUS
40 NtfsCleanupFile(PDEVICE_EXTENSION DeviceExt,
41 PFILE_OBJECT FileObject,
42 BOOLEAN CanWait)
43 {
44 PNTFS_FCB Fcb;
45
46 DPRINT("NtfsCleanupFile(DeviceExt %p, FileObject %p, CanWait %u)\n",
47 DeviceExt,
48 FileObject,
49 CanWait);
50
51 Fcb = (PNTFS_FCB)(FileObject->FsContext);
52 if (!Fcb)
53 return STATUS_SUCCESS;
54
55 if (Fcb->Flags & FCB_IS_VOLUME)
56 {
57 Fcb->OpenHandleCount--;
58
59 if (Fcb->OpenHandleCount != 0)
60 {
61 // Remove share access when handled
62 }
63 }
64 else
65 {
66 if (!ExAcquireResourceExclusiveLite(&Fcb->MainResource, CanWait))
67 {
68 return STATUS_PENDING;
69 }
70
71 Fcb->OpenHandleCount--;
72
73 CcUninitializeCacheMap(FileObject, &Fcb->RFCB.FileSize, NULL);
74
75 if (Fcb->OpenHandleCount != 0)
76 {
77 // Remove share access when handled
78 }
79
80 FileObject->Flags |= FO_CLEANUP_COMPLETE;
81
82 ExReleaseResourceLite(&Fcb->MainResource);
83 }
84
85 return STATUS_SUCCESS;
86 }
87
88 NTSTATUS
89 NtfsCleanup(PNTFS_IRP_CONTEXT IrpContext)
90 {
91 PDEVICE_EXTENSION DeviceExtension;
92 PFILE_OBJECT FileObject;
93 NTSTATUS Status;
94 PDEVICE_OBJECT DeviceObject;
95
96 DPRINT("NtfsCleanup() called\n");
97
98 DeviceObject = IrpContext->DeviceObject;
99 if (DeviceObject == NtfsGlobalData->DeviceObject)
100 {
101 DPRINT("Cleaning up file system\n");
102 IrpContext->Irp->IoStatus.Information = 0;
103 return STATUS_SUCCESS;
104 }
105
106 FileObject = IrpContext->FileObject;
107 DeviceExtension = DeviceObject->DeviceExtension;
108
109 Status = NtfsCleanupFile(DeviceExtension, FileObject, BooleanFlagOn(IrpContext->Flags, IRPCONTEXT_CANWAIT));
110 if (Status == STATUS_PENDING)
111 {
112 return NtfsMarkIrpContextForQueue(IrpContext);
113 }
114
115 IrpContext->Irp->IoStatus.Information = 0;
116 return Status;
117 }