[FFS]
[reactos.git] / reactos / drivers / filesystems / ffs / src / lock.c
1 /*
2 * FFS File System Driver for Windows
3 *
4 * lock.c
5 *
6 * 2004.5.6 ~
7 *
8 * Lee Jae-Hong, http://www.pyrasis.com
9 *
10 * See License.txt
11 *
12 */
13
14 #include "ntifs.h"
15 #include "ffsdrv.h"
16
17 /* Globals */
18
19 extern PFFS_GLOBAL FFSGlobal;
20
21
22 /* Definitions */
23
24 #ifdef ALLOC_PRAGMA
25 #pragma alloc_text(PAGE, FFSLockControl)
26 #endif
27
28 __drv_mustHoldCriticalRegion
29 NTSTATUS
30 FFSLockControl(
31 IN PFFS_IRP_CONTEXT IrpContext)
32 {
33 PDEVICE_OBJECT DeviceObject;
34 BOOLEAN CompleteRequest;
35 NTSTATUS Status = STATUS_UNSUCCESSFUL;
36 PFILE_OBJECT FileObject;
37 PFFS_FCB Fcb;
38 PIRP Irp;
39
40 PAGED_CODE();
41
42 _SEH2_TRY
43 {
44 ASSERT(IrpContext != NULL);
45
46 ASSERT((IrpContext->Identifier.Type == FFSICX) &&
47 (IrpContext->Identifier.Size == sizeof(FFS_IRP_CONTEXT)));
48
49 DeviceObject = IrpContext->DeviceObject;
50
51 if (DeviceObject == FFSGlobal->DeviceObject)
52 {
53 CompleteRequest = TRUE;
54 Status = STATUS_INVALID_DEVICE_REQUEST;
55 _SEH2_LEAVE;
56 }
57
58 FileObject = IrpContext->FileObject;
59
60 Fcb = (PFFS_FCB)FileObject->FsContext;
61
62 ASSERT(Fcb != NULL);
63
64 if (Fcb->Identifier.Type == FFSVCB)
65 {
66 CompleteRequest = TRUE;
67 Status = STATUS_INVALID_PARAMETER;
68 _SEH2_LEAVE;
69 }
70
71 ASSERT((Fcb->Identifier.Type == FFSFCB) &&
72 (Fcb->Identifier.Size == sizeof(FFS_FCB)));
73
74 if (FlagOn(Fcb->FFSMcb->FileAttr, FILE_ATTRIBUTE_DIRECTORY))
75 {
76 CompleteRequest = TRUE;
77 Status = STATUS_INVALID_PARAMETER;
78 _SEH2_LEAVE;
79 }
80
81 Irp = IrpContext->Irp;
82
83 //
84 // While the file has any byte range locks we set IsFastIoPossible to
85 // FastIoIsQuestionable so that the FastIoCheckIfPossible function is
86 // called to check the locks for any fast I/O read/write operation.
87 //
88 if (Fcb->Header.IsFastIoPossible != FastIoIsQuestionable)
89 {
90
91 FFSPrint((DBG_INFO,
92 "FFSLockControl: %-16.16s %-31s %s\n",
93 FFSGetCurrentProcessName(),
94 "FastIoIsQuestionable",
95 Fcb->AnsiFileName.Buffer));
96
97 Fcb->Header.IsFastIoPossible = FastIoIsQuestionable;
98 }
99
100 //
101 // FsRtlProcessFileLock acquires FileObject->FsContext->Resource while
102 // modifying the file locks and calls IoCompleteRequest when it's done.
103 //
104
105 CompleteRequest = FALSE;
106
107 Status = FsRtlProcessFileLock(
108 &Fcb->FileLockAnchor,
109 Irp,
110 NULL);
111
112 if (!NT_SUCCESS(Status))
113 {
114 FFSPrint((DBG_ERROR,
115 "FFSLockControl: %-16.16s %-31s *** Status: %s (%#x) ***\n",
116 FFSGetCurrentProcessName(),
117 "IRP_MJ_LOCK_CONTROL",
118 FFSNtStatusToString(Status),
119 Status));
120 }
121 }
122
123 _SEH2_FINALLY
124 {
125 if (!IrpContext->ExceptionInProgress)
126 {
127 if (!CompleteRequest)
128 {
129 IrpContext->Irp = NULL;
130 }
131
132 FFSCompleteIrpContext(IrpContext, Status);
133 }
134 } _SEH2_END;
135
136 return Status;
137 }