0e58603de70e89da23006436ddef20109b994817
[openafs.git] / src / WINNT / afsrdr / kernel / lib / AFSFlushBuffers.cpp
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 Kernel Drivers, LLC.
3  * Copyright (c) 2009, 2010, 2011 Your File System, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright notice,
11  *   this list of conditions and the following disclaimer.
12  * - Redistributions in binary form must reproduce the above copyright
13  *   notice,
14  *   this list of conditions and the following disclaimer in the
15  *   documentation
16  *   and/or other materials provided with the distribution.
17  * - Neither the names of Kernel Drivers, LLC and Your File System, Inc.
18  *   nor the names of their contributors may be used to endorse or promote
19  *   products derived from this software without specific prior written
20  *   permission from Kernel Drivers, LLC and Your File System, Inc.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
26  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 //
36 // File: AFSFlushBuffers.cpp
37 //
38
39 #include "AFSCommon.h"
40
41 NTSTATUS
42 AFSFlushBuffers( IN PDEVICE_OBJECT LibDeviceObject,
43                  IN PIRP Irp)
44 {
45
46     UNREFERENCED_PARAMETER(LibDeviceObject);
47     NTSTATUS           ntStatus = STATUS_SUCCESS;
48     IO_STACK_LOCATION *pIrpSp = IoGetCurrentIrpStackLocation( Irp);
49     PFILE_OBJECT       pFileObject = pIrpSp->FileObject;
50     AFSFcb            *pFcb = (AFSFcb *)pFileObject->FsContext;
51     AFSCcb            *pCcb = (AFSCcb *)pFileObject->FsContext2;
52     IO_STATUS_BLOCK    iosb = {0};
53     BOOLEAN            bReleaseSectionObject = FALSE;
54
55     pIrpSp = IoGetCurrentIrpStackLocation( Irp);
56
57     __Enter
58     {
59
60         if( pFcb == NULL)
61         {
62
63             AFSDbgTrace(( AFS_SUBSYSTEM_FILE_PROCESSING,
64                           AFS_TRACE_LEVEL_ERROR,
65                           "AFSFlushBuffers Attempted access (%p) when pFcb == NULL\n",
66                           Irp));
67
68             try_return( ntStatus = STATUS_INVALID_DEVICE_REQUEST);
69         }
70
71         if( pFcb->Header.NodeTypeCode == AFS_ROOT_FCB ||
72             pFcb->Header.NodeTypeCode == AFS_ROOT_ALL )
73         {
74
75             //
76             // Once we support ADS's on directories we need to perform a flush ehre
77             //
78
79             try_return( ntStatus = STATUS_SUCCESS);
80
81         }
82         else if (pFcb->Header.NodeTypeCode != AFS_FILE_FCB)
83         {
84             //
85             // Nothing to flush Everything but files are write through
86             //
87             try_return( ntStatus = STATUS_INVALID_PARAMETER);
88         }
89
90         AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING|AFS_SUBSYSTEM_SECTION_OBJECT,
91                       AFS_TRACE_LEVEL_VERBOSE,
92                       "AFSFlushBuffers Acquiring Fcb SectionObject lock %p SHARED %08lX\n",
93                       &pFcb->NPFcb->SectionObjectResource,
94                       PsGetCurrentThread()));
95
96         AFSAcquireShared( &pFcb->NPFcb->SectionObjectResource,
97                           TRUE);
98
99         bReleaseSectionObject = TRUE;
100
101         //
102         // The flush consists of two parts.  We firstly flush our
103         // cache (if we have one), then we tell the service to write
104         // to the remote server
105         //
106         __try
107         {
108
109             CcFlushCache( &pFcb->NPFcb->SectionObjectPointers, NULL, 0, &iosb);
110
111             if (!NT_SUCCESS( iosb.Status ))
112             {
113
114                 AFSDbgTrace(( AFS_SUBSYSTEM_IO_PROCESSING,
115                               AFS_TRACE_LEVEL_ERROR,
116                               "AFSFlushBuffers CcFlushCache [1] failure FID %08lX-%08lX-%08lX-%08lX Status 0x%08lX Bytes 0x%08lX\n",
117                               pFcb->ObjectInformation->FileId.Cell,
118                               pFcb->ObjectInformation->FileId.Volume,
119                               pFcb->ObjectInformation->FileId.Vnode,
120                               pFcb->ObjectInformation->FileId.Unique,
121                               iosb.Status,
122                               iosb.Information));
123
124                 try_return( ntStatus = iosb.Status );
125             }
126         }
127         __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()))
128         {
129
130             try_return( ntStatus = GetExceptionCode());
131         }
132
133         AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING|AFS_SUBSYSTEM_SECTION_OBJECT,
134                       AFS_TRACE_LEVEL_VERBOSE,
135                       "AFSFlushBuffers Releasing Fcb SectionObject lock %p SHARED %08lX\n",
136                       &pFcb->NPFcb->SectionObjectResource,
137                       PsGetCurrentThread()));
138
139         AFSReleaseResource( &pFcb->NPFcb->SectionObjectResource);
140
141         bReleaseSectionObject = FALSE;
142
143         //
144         // Now, flush to the server - if there is stuff to do
145         //
146
147         ntStatus = AFSFlushExtents( pFcb,
148                                     &pCcb->AuthGroup);
149
150         if( !NT_SUCCESS( ntStatus))
151         {
152
153             AFSReleaseExtentsWithFlush( pFcb,
154                                         &pCcb->AuthGroup,
155                                         TRUE);
156
157             ntStatus = STATUS_SUCCESS;
158         }
159
160 try_exit:
161
162         if ( bReleaseSectionObject)
163         {
164
165             AFSDbgTrace(( AFS_SUBSYSTEM_LOCK_PROCESSING|AFS_SUBSYSTEM_SECTION_OBJECT,
166                           AFS_TRACE_LEVEL_VERBOSE,
167                           "AFSFlushBuffers Releasing Fcb SectionObject lock %p SHARED %08lX\n",
168                           &pFcb->NPFcb->SectionObjectResource,
169                           PsGetCurrentThread()));
170
171             AFSReleaseResource( &pFcb->NPFcb->SectionObjectResource);
172         }
173
174         AFSCompleteRequest( Irp, ntStatus);
175     }
176
177     return ntStatus;
178 }