Windows: shuffle trace messages for clarity
[openafs.git] / src / WINNT / afsrdr / kernel / lib / AFSExtentsSupport.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: AFSCommSupport.cpp
37 //
38 #include "AFSCommon.h"
39
40 #define AFS_MAX_FCBS_TO_DROP 10
41
42 static ULONG ExtentsMasks[AFS_NUM_EXTENT_LISTS] = AFS_EXTENTS_MASKS;
43 static VOID VerifyExtentsLists(AFSFcb *Fcb);
44 static AFSExtent *DirtyExtentFor(PLIST_ENTRY le);
45
46 LIST_ENTRY *
47 AFSEntryForOffset( IN AFSFcb *Fcb,
48                    IN PLARGE_INTEGER Offset);
49
50
51 //
52 // Returns with Extents lock EX and no one using them.
53 //
54
55 VOID
56 AFSLockForExtentsTrim( IN AFSFcb *Fcb)
57 {
58
59     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
60                   AFS_TRACE_LEVEL_VERBOSE,
61                   "AFSLockForExtentsTrim Acquiring Fcb extents lock %08lX EXCL %08lX\n",
62                   &Fcb->NPFcb->Specific.File.ExtentsResource,
63                   PsGetCurrentThread());
64
65     AFSAcquireExcl( &Fcb->NPFcb->Specific.File.ExtentsResource, TRUE );
66
67     return;
68 }
69
70 //
71 // return FALSE *or* with Extents lock EX and noone using them
72 //
73 BOOLEAN
74 AFSLockForExtentsTrimNoWait( IN AFSFcb *Fcb)
75 {
76
77     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
78                   AFS_TRACE_LEVEL_VERBOSE,
79                   "AFSLockForExtentsTrimNoWait Attempting to acquire Fcb extent lock %08lX EXCL %08lX\n",
80                   &Fcb->NPFcb->Specific.File.ExtentsResource,
81                   PsGetCurrentThread());
82
83     if (!AFSAcquireExcl( &Fcb->NPFcb->Specific.File.ExtentsResource, FALSE ))
84     {
85         //
86         // Couldn't lock immediately
87         //
88
89         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
90                       AFS_TRACE_LEVEL_VERBOSE,
91                       "AFSLockForExtentsTrimNoWait Refused to wait for Fcb extent lock %08lX EXCL %08lX\n",
92                       &Fcb->NPFcb->Specific.File.ExtentsResource,
93                       PsGetCurrentThread());
94
95         return FALSE;
96     }
97
98     return TRUE;
99 }
100
101 static VOID
102 AFSFreeExtent( IN AFSFcb *Fcb,
103                IN AFSExtent *pExtent)
104 {
105     AFSDeviceExt        *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
106     LONG                 lCount;
107
108     for (ULONG i = 0; i < AFS_NUM_EXTENT_LISTS; i ++)
109     {
110         if (NULL != pExtent->Lists[i].Flink && !IsListEmpty(&pExtent->Lists[i]))
111         {
112             RemoveEntryList( &pExtent->Lists[i] );
113         }
114     }
115
116     InterlockedExchangeAdd( &pControlDevExt->Specific.Control.ExtentsHeldLength, -((LONG)(pExtent->Size/1024)));
117
118     InterlockedExchangeAdd( &Fcb->Specific.File.ExtentLength, -((LONG)(pExtent->Size/1024)));
119
120     lCount = InterlockedDecrement( &Fcb->Specific.File.ExtentCount);
121
122     lCount = InterlockedDecrement( &pControlDevExt->Specific.Control.ExtentCount);
123
124     if( lCount == 0)
125     {
126
127         KeSetEvent( &pControlDevExt->Specific.Control.ExtentsHeldEvent,
128                     0,
129                     FALSE);
130     }
131
132     AFSExFreePoolWithTag( pExtent, AFS_EXTENT_TAG);
133 }
134
135 //
136 // AFSTearDownFcbExtents was originally written to
137 // remove all of the extents from an FCB.  For that to happen
138 // it must be an invariant that the extent list cannot change
139 // from the moment the caller decides to execute AFSTearDownFcbExtents
140 // until it returns.  This invariant does not hold because the
141 // the decision to call AFSTearDownFcbExtents is made without
142 // holding the ExtentsResource and it is possible that extents
143 // are in active use. Therefore, AFSTearDownFcbExtents now releases
144 // as many non-active extents as it can.
145 //
146 VOID
147 AFSTearDownFcbExtents( IN AFSFcb *Fcb,
148                        IN GUID *AuthGroup)
149 {
150     AFSNonPagedFcb      *pNPFcb = Fcb->NPFcb;
151     LIST_ENTRY          *le, *leNext;
152     AFSExtent           *pEntry;
153     LONG                 lExtentCount = 0, lProcessCount = 0;
154     LONG                 lFcbExtentCount;
155     ULONG                ulReleaseCount = 0;
156     size_t               sz;
157     AFSReleaseExtentsCB *pRelease = NULL;
158     BOOLEAN              locked = FALSE;
159     NTSTATUS             ntStatus;
160     AFSDeviceExt        *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
161     GUID                *pAuthGroup = AuthGroup;
162     GUID                 stAuthGroup;
163     LONG                 lCount;
164
165     __Enter
166     {
167
168         if( pAuthGroup == NULL ||
169             RtlCompareMemory( pAuthGroup,
170                               &Fcb->NPFcb->Specific.File.ExtentsRequestAuthGroup,
171                               sizeof( GUID)) == sizeof( GUID))
172         {
173
174             RtlZeroMemory( &stAuthGroup,
175                            sizeof( GUID));
176
177             ntStatus = AFSRetrieveValidAuthGroup( Fcb,
178                                                   NULL,
179                                                   TRUE,
180                                                   &stAuthGroup);
181
182             if( !NT_SUCCESS( ntStatus))
183             {
184                 try_return( ntStatus);
185             }
186
187             pAuthGroup = &stAuthGroup;
188         }
189
190         //
191         // Ensure that no one is working with the extents and grab the
192         // lock
193         //
194
195         AFSLockForExtentsTrim( Fcb );
196
197         locked = TRUE;
198
199         if (0 == Fcb->Specific.File.ExtentCount)
200         {
201             try_return ( ntStatus = STATUS_SUCCESS);
202         }
203
204         sz = sizeof( AFSReleaseExtentsCB ) + (AFS_MAXIMUM_EXTENT_RELEASE_COUNT * sizeof ( AFSFileExtentCB ));
205
206         pRelease = (AFSReleaseExtentsCB*) AFSExAllocatePoolWithTag( NonPagedPool,
207                                                                     sz,
208                                                                     AFS_EXTENT_RELEASE_TAG);
209         if (NULL == pRelease)
210         {
211
212             try_return ( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
213         }
214
215         AFSAcquireExcl( &pNPFcb->Specific.File.DirtyExtentsListLock,
216                         TRUE);
217
218         for( le = Fcb->Specific.File.ExtentsLists[AFS_EXTENTS_LIST].Flink,
219              lExtentCount = 0,
220              lFcbExtentCount = Fcb->Specific.File.ExtentCount;
221              lExtentCount < lFcbExtentCount;
222              lExtentCount += lProcessCount)
223         {
224
225             RtlZeroMemory( pRelease,
226                            sizeof( AFSReleaseExtentsCB ) +
227                            (AFS_MAXIMUM_EXTENT_RELEASE_COUNT * sizeof ( AFSFileExtentCB )));
228
229             for( lProcessCount = 0, ulReleaseCount = 0;
230                  !IsListEmpty( le) &&
231                  ulReleaseCount < AFS_MAXIMUM_EXTENT_RELEASE_COUNT &&
232                  lExtentCount + lProcessCount < lFcbExtentCount;
233                  lProcessCount++, le = leNext)
234             {
235
236                 leNext = le->Flink;
237
238                 pEntry = ExtentFor( le, AFS_EXTENTS_LIST );
239
240                 if( pEntry->ActiveCount == 0)
241                 {
242
243                     pRelease->FileExtents[ulReleaseCount].Flags = AFS_EXTENT_FLAG_RELEASE;
244
245 #if GEN_MD5
246                     RtlCopyMemory( pRelease->FileExtents[ulReleaseCount].MD5,
247                                    pEntry->MD5,
248                                    sizeof(pEntry->MD5));
249
250                     pRelease->FileExtents[ulReleaseCount].Flags |= AFS_EXTENT_FLAG_MD5_SET;
251 #endif
252
253                     if( BooleanFlagOn( pEntry->Flags, AFS_EXTENT_DIRTY))
254                     {
255
256                         LONG dirtyCount;
257
258                         AFSRemoveEntryDirtyList( Fcb,
259                                                  pEntry);
260
261                         pRelease->FileExtents[ulReleaseCount].Flags |= AFS_EXTENT_FLAG_DIRTY;
262
263                         dirtyCount = InterlockedDecrement( &Fcb->Specific.File.ExtentsDirtyCount);
264
265                         ASSERT( dirtyCount >= 0);
266                     }
267
268                     AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
269                                   AFS_TRACE_LEVEL_VERBOSE,
270                                   "AFSTearDownFcbExtents Releasing extent %p fid %08lX-%08lX-%08lX-%08lX Offset %08lX-%08lX Len %08lX\n",
271                                   pEntry,
272                                   Fcb->ObjectInformation->FileId.Cell,
273                                   Fcb->ObjectInformation->FileId.Volume,
274                                   Fcb->ObjectInformation->FileId.Vnode,
275                                   Fcb->ObjectInformation->FileId.Unique,
276                                   pEntry->FileOffset.HighPart,
277                                   pEntry->FileOffset.LowPart,
278                                   pEntry->Size);
279
280                     pRelease->FileExtents[ulReleaseCount].Length = pEntry->Size;
281                     pRelease->FileExtents[ulReleaseCount].DirtyLength = pEntry->Size;
282                     pRelease->FileExtents[ulReleaseCount].DirtyOffset = 0;
283                     pRelease->FileExtents[ulReleaseCount].CacheOffset = pEntry->CacheOffset;
284                     pRelease->FileExtents[ulReleaseCount].FileOffset = pEntry->FileOffset;
285
286                     ulReleaseCount++;
287
288                     AFSFreeExtent( Fcb,
289                                    pEntry);
290                 }
291             }
292
293             if ( ulReleaseCount > 0)
294             {
295
296                 pRelease->ExtentCount = ulReleaseCount;
297
298                 pRelease->Flags = AFS_EXTENT_FLAG_RELEASE;
299
300                 //
301                 // Update the metadata for this call
302                 //
303
304                 pRelease->AllocationSize = Fcb->ObjectInformation->EndOfFile;
305                 pRelease->CreateTime = Fcb->ObjectInformation->CreationTime;
306                 pRelease->ChangeTime = Fcb->ObjectInformation->ChangeTime;
307                 pRelease->LastAccessTime = Fcb->ObjectInformation->LastAccessTime;
308                 pRelease->LastWriteTime = Fcb->ObjectInformation->LastWriteTime;
309
310                 //
311                 // Send the request down.  We cannot send this down
312                 // asynchronously - if we did that we could request them
313                 // back before the service got this request and then this
314                 // request would be a corruption.
315                 //
316
317                 sz = sizeof( AFSReleaseExtentsCB ) + (lProcessCount * sizeof ( AFSFileExtentCB ));
318
319                 ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_RELEASE_FILE_EXTENTS,
320                                               AFS_REQUEST_FLAG_SYNCHRONOUS,
321                                               pAuthGroup,
322                                               NULL,
323                                               &Fcb->ObjectInformation->FileId,
324                                               pRelease,
325                                               sz,
326                                               NULL,
327                                               NULL);
328
329                 if( !NT_SUCCESS(ntStatus))
330                 {
331
332                     //
333                     // Regardless of whether or not the AFSProcessRequest() succeeded, the extents
334                     // were released (if AFS_EXTENT_FLAG_RELEASE was set).  Log the error so it is known.
335                     //
336
337                     AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
338                                   AFS_TRACE_LEVEL_ERROR,
339                                   "AFSTearDownFcbExtents AFS_REQUEST_TYPE_RELEASE_FILE_EXTENTS failed fid %08lX-%08lX-%08lX-%08lX Status %08lX\n",
340                                   Fcb->ObjectInformation->FileId.Cell,
341                                   Fcb->ObjectInformation->FileId.Volume,
342                                   Fcb->ObjectInformation->FileId.Vnode,
343                                   Fcb->ObjectInformation->FileId.Unique,
344                                   ntStatus);
345
346                 }
347             }
348         }
349
350         AFSReleaseResource( &pNPFcb->Specific.File.DirtyExtentsListLock);
351
352         //
353         // if all extents have been released, reinitialize the skip lists
354         //
355
356         if( Fcb->Specific.File.ExtentCount == 0)
357         {
358
359             for (ULONG i = 0; i < AFS_NUM_EXTENT_LISTS; i++)
360             {
361                 InitializeListHead(&Fcb->Specific.File.ExtentsLists[i]);
362             }
363
364             //
365             // Reinitialize the dirty list as well
366             //
367
368             AFSAcquireExcl( &pNPFcb->Specific.File.DirtyExtentsListLock,
369                             TRUE);
370
371             ASSERT( Fcb->Specific.File.ExtentsDirtyCount == 0);
372
373             Fcb->NPFcb->Specific.File.DirtyListHead = NULL;
374             Fcb->NPFcb->Specific.File.DirtyListTail = NULL;
375
376             AFSReleaseResource( &pNPFcb->Specific.File.DirtyExtentsListLock);
377         }
378
379         Fcb->NPFcb->Specific.File.ExtentsRequestStatus = STATUS_SUCCESS;
380
381 try_exit:
382
383         if (locked)
384         {
385
386             AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
387                           AFS_TRACE_LEVEL_VERBOSE,
388                           "AFSTearDownFcbExtents Releasing Fcb extent lock %08lX thread %08lX\n",
389                           &Fcb->NPFcb->Specific.File.ExtentsResource,
390                           PsGetCurrentThread());
391
392             AFSReleaseResource( &Fcb->NPFcb->Specific.File.ExtentsResource );
393         }
394
395         if (pRelease)
396         {
397
398             AFSExFreePoolWithTag( pRelease, AFS_EXTENT_RELEASE_TAG);
399         }
400     }
401 }
402
403 VOID
404 AFSDeleteFcbExtents( IN AFSFcb *Fcb)
405 {
406     AFSNonPagedFcb      *pNPFcb = Fcb->NPFcb;
407     LIST_ENTRY          *le, *leNext;
408     AFSExtent           *pEntry;
409     LONG                 lExtentCount = 0, lProcessCount = 0;
410     LONG                 lFcbExtentCount;
411     size_t               sz;
412     BOOLEAN              locked = FALSE;
413     NTSTATUS             ntStatus;
414     AFSDeviceExt        *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
415     LONG                 lCount;
416
417     __Enter
418     {
419
420         //
421         // Ensure that no one is working with the extents and grab the
422         // lock
423         //
424
425         AFSLockForExtentsTrim( Fcb );
426
427         locked = TRUE;
428
429         if (0 == Fcb->Specific.File.ExtentCount)
430         {
431             try_return ( ntStatus = STATUS_SUCCESS);
432         }
433
434         sz = sizeof( AFSReleaseExtentsCB ) + (AFS_MAXIMUM_EXTENT_RELEASE_COUNT * sizeof ( AFSFileExtentCB ));
435
436         AFSAcquireExcl( &pNPFcb->Specific.File.DirtyExtentsListLock,
437                         TRUE);
438
439         for( le = Fcb->Specific.File.ExtentsLists[AFS_EXTENTS_LIST].Flink,
440              lExtentCount = 0,
441              lFcbExtentCount = Fcb->Specific.File.ExtentCount;
442              lExtentCount < lFcbExtentCount;
443              lExtentCount += lProcessCount)
444         {
445
446             for( lProcessCount = 0;
447                  !IsListEmpty( le) &&
448                  lExtentCount + lProcessCount < lFcbExtentCount;
449                  lProcessCount++, le = leNext)
450             {
451
452                 leNext = le->Flink;
453
454                 pEntry = ExtentFor( le, AFS_EXTENTS_LIST );
455
456                 if( BooleanFlagOn( pEntry->Flags, AFS_EXTENT_DIRTY))
457                 {
458
459                     LONG dirtyCount;
460
461                     AFSRemoveEntryDirtyList( Fcb,
462                                              pEntry);
463
464                     dirtyCount = InterlockedDecrement( &Fcb->Specific.File.ExtentsDirtyCount);
465
466                     ASSERT( dirtyCount >= 0);
467                 }
468
469                 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
470                               AFS_TRACE_LEVEL_VERBOSE,
471                               "AFSDeleteFcbExtents Deleting extent %p fid %08lX-%08lX-%08lX-%08lX Offset %08lX-%08lX Len %08lX\n",
472                               pEntry,
473                               Fcb->ObjectInformation->FileId.Cell,
474                               Fcb->ObjectInformation->FileId.Volume,
475                               Fcb->ObjectInformation->FileId.Vnode,
476                               Fcb->ObjectInformation->FileId.Unique,
477                               pEntry->FileOffset.HighPart,
478                               pEntry->FileOffset.LowPart,
479                               pEntry->Size);
480
481                 AFSFreeExtent( Fcb,
482                                pEntry);
483             }
484         }
485
486         AFSReleaseResource( &pNPFcb->Specific.File.DirtyExtentsListLock);
487
488         //
489         // if all extents have been released, reinitialize the skip lists
490         //
491
492         ASSERT( Fcb->Specific.File.ExtentCount == 0);
493
494         if( Fcb->Specific.File.ExtentCount == 0)
495         {
496
497             for (ULONG i = 0; i < AFS_NUM_EXTENT_LISTS; i++)
498             {
499                 InitializeListHead(&Fcb->Specific.File.ExtentsLists[i]);
500             }
501
502             //
503             // Reinitialize the dirty list as well
504             //
505
506             AFSAcquireExcl( &pNPFcb->Specific.File.DirtyExtentsListLock,
507                             TRUE);
508
509             ASSERT( Fcb->Specific.File.ExtentsDirtyCount == 0);
510
511             Fcb->NPFcb->Specific.File.DirtyListHead = NULL;
512             Fcb->NPFcb->Specific.File.DirtyListTail = NULL;
513
514             AFSReleaseResource( &pNPFcb->Specific.File.DirtyExtentsListLock);
515         }
516
517         Fcb->NPFcb->Specific.File.ExtentsRequestStatus = STATUS_SUCCESS;
518
519 try_exit:
520
521         if (locked)
522         {
523
524             AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
525                           AFS_TRACE_LEVEL_VERBOSE,
526                           "AFSDeleteFcbExtents Releasing Fcb extent lock %08lX thread %08lX\n",
527                           &Fcb->NPFcb->Specific.File.ExtentsResource,
528                           PsGetCurrentThread());
529
530             AFSReleaseResource( &Fcb->NPFcb->Specific.File.ExtentsResource );
531         }
532     }
533 }
534
535
536 static PAFSExtent
537 ExtentForOffsetInList( IN AFSFcb *Fcb,
538                        IN LIST_ENTRY *List,
539                        IN ULONG ListNumber,
540                        IN PLARGE_INTEGER Offset)
541 {
542     //
543     // Return the extent that maps the offset, that
544     //   - Contains the offset
545     //   - or is immediately ahead of the offset (in this list)
546     //   - otherwise return NULL.
547     //
548
549     PLIST_ENTRY  pLe = List;
550     AFSExtent   *pPrevious = NULL;
551
552     ASSERT( ExIsResourceAcquiredLite( &Fcb->NPFcb->Specific.File.ExtentsResource ));
553
554     while (pLe != &Fcb->Specific.File.ExtentsLists[ListNumber])
555     {
556         AFSExtent *entry;
557
558         entry = ExtentFor( pLe, ListNumber );
559
560         if( entry == NULL)
561         {
562             return entry;
563         }
564
565         if (Offset->QuadPart < entry->FileOffset.QuadPart)
566         {
567             //
568             // Offset is ahead of entry.  Return previous
569             //
570             return pPrevious;
571         }
572
573         if (Offset->QuadPart >= (entry->FileOffset.QuadPart + entry->Size))
574         {
575             //
576             // We start after this extent - carry on round
577             //
578             pPrevious = entry;
579             pLe = pLe->Flink;
580             continue;
581         }
582
583         //
584         // Otherwise its a match
585         //
586
587         return entry;
588     }
589
590     //
591     // Got to the end.  Return Previous
592     //
593     return pPrevious;
594 }
595
596 BOOLEAN
597 AFSExtentContains( IN AFSExtent *Extent, IN PLARGE_INTEGER Offset)
598 {
599     if (NULL == Extent)
600     {
601         return FALSE;
602     }
603     return (Extent->FileOffset.QuadPart <= Offset->QuadPart &&
604             (Extent->FileOffset.QuadPart + Extent->Size) > Offset->QuadPart);
605 }
606
607
608 //
609 // Return the extent that contains the offset
610 //
611 PAFSExtent
612 AFSExtentForOffsetHint( IN AFSFcb *Fcb,
613                         IN PLARGE_INTEGER Offset,
614                         IN BOOLEAN ReturnPrevious,
615                         IN AFSExtent *Hint)
616 {
617     AFSExtent *pPrevious = Hint;
618     LIST_ENTRY *pLe;
619     LONG i;
620
621     ASSERT( ExIsResourceAcquiredLite( &Fcb->NPFcb->Specific.File.ExtentsResource ));
622
623 #if AFS_VALIDATE_EXTENTS
624     VerifyExtentsLists(Fcb);
625 #endif
626
627     //
628     // So we will go across the skip lists until we find an
629     // appropriate entry (previous or direct match).  If it's a match
630     // we are done, other wise we start on the next layer down
631     //
632     for (i = AFS_NUM_EXTENT_LISTS-1; i >= AFS_EXTENTS_LIST; i--)
633     {
634         if (NULL == pPrevious)
635         {
636             //
637             // We haven't found anything in the previous layers
638             //
639             pLe = Fcb->Specific.File.ExtentsLists[i].Flink;
640         }
641         else if (NULL == pPrevious->Lists[i].Flink)
642         {
643             ASSERT(AFS_EXTENTS_LIST != i);
644             //
645             // The hint doesn't exist at this level, next one down
646             //
647             continue;
648         }
649         else
650         {
651             //
652             // take the previous into the next
653             //
654             pLe = &pPrevious->Lists[i];
655         }
656
657         pPrevious = ExtentForOffsetInList( Fcb, pLe, i, Offset);
658
659         if (NULL != pPrevious && AFSExtentContains(pPrevious, Offset))
660         {
661             //
662             // Found it immediately.  Stop here
663             //
664             return pPrevious;
665         }
666     }
667
668     if (NULL == pPrevious || ReturnPrevious )
669     {
670         return pPrevious;
671     }
672
673     ASSERT( !AFSExtentContains(pPrevious, Offset) );
674
675     return NULL;
676 }
677
678 LIST_ENTRY *
679 AFSEntryForOffset( IN AFSFcb *Fcb,
680                    IN PLARGE_INTEGER Offset)
681 {
682     AFSExtent *pPrevious = NULL;
683     LIST_ENTRY *pLe;
684     LONG i;
685
686     ASSERT( ExIsResourceAcquiredLite( &Fcb->NPFcb->Specific.File.ExtentsResource ));
687
688 #if AFS_VALIDATE_EXTENTS
689     VerifyExtentsLists(Fcb);
690 #endif
691
692     //
693     // So we will go across the skip lists until we find an
694     // appropriate entry (previous or direct match).  If it's a match
695     // we are done, other wise we start on the next layer down
696     //
697     for (i = AFS_NUM_EXTENT_LISTS-1; i >= AFS_EXTENTS_LIST; i--)
698     {
699         if (NULL == pPrevious)
700         {
701             //
702             // We haven't found anything in the previous layers
703             //
704             pLe = Fcb->Specific.File.ExtentsLists[i].Flink;
705         }
706         else if (NULL == pPrevious->Lists[i].Flink)
707         {
708             ASSERT(AFS_EXTENTS_LIST != i);
709             //
710             // The hint doesn't exist at this level, next one down
711             //
712             continue;
713         }
714         else
715         {
716             //
717             // take the previous into the next
718             //
719             pLe = &pPrevious->Lists[i];
720         }
721
722         pPrevious = ExtentForOffsetInList( Fcb, pLe, i, Offset);
723
724         if (NULL != pPrevious && AFSExtentContains(pPrevious, Offset))
725         {
726             //
727             // Found it immediately.  Stop here
728             //
729             return pLe;
730         }
731     }
732
733     return NULL;
734 }
735
736 PAFSExtent
737 AFSExtentForOffset( IN AFSFcb *Fcb,
738                     IN PLARGE_INTEGER Offset,
739                     IN BOOLEAN ReturnPrevious)
740 {
741     return AFSExtentForOffsetHint(Fcb, Offset, ReturnPrevious, NULL);
742 }
743
744
745 BOOLEAN AFSDoExtentsMapRegion(IN AFSFcb *Fcb,
746                               IN PLARGE_INTEGER Offset,
747                               IN ULONG Size,
748                               IN OUT AFSExtent **FirstExtent,
749                               OUT AFSExtent **LastExtent)
750 {
751     //
752     // Return TRUE region is completely mapped.  FALSE
753     // otherwise.  If the region isn't mapped then the last
754     // extent to map part of the region is returned.
755     //
756     // *LastExtent as input is where to start looking.
757     // *LastExtent as output is either the extent which
758     //  contains the Offset, or the last one which doesn't
759     //
760     AFSExtent *entry;
761     AFSExtent *newEntry;
762     BOOLEAN retVal = FALSE;
763
764     __Enter
765     {
766
767         ASSERT( ExIsResourceAcquiredLite( &Fcb->NPFcb->Specific.File.ExtentsResource ));
768
769         __try
770         {
771             entry = AFSExtentForOffsetHint(Fcb, Offset, TRUE, *FirstExtent);
772             *FirstExtent = entry;
773
774             if (NULL == entry || !AFSExtentContains(entry, Offset))
775             {
776                 try_return (retVal = FALSE);
777             }
778
779             ASSERT(Offset->QuadPart >= entry->FileOffset.QuadPart);
780
781             while (TRUE)
782             {
783                 if ((entry->FileOffset.QuadPart + entry->Size) >=
784                      (Offset->QuadPart + Size))
785                 {
786                     //
787                     // The end is inside the extent
788                     //
789                     try_return (retVal = TRUE);
790                 }
791
792                 if (entry->Lists[AFS_EXTENTS_LIST].Flink == &Fcb->Specific.File.ExtentsLists[AFS_EXTENTS_LIST])
793                 {
794                     //
795                     // Run out of extents
796                     //
797                     try_return (retVal = FALSE);
798                 }
799
800                 newEntry = NextExtent( entry, AFS_EXTENTS_LIST );
801
802                 if (newEntry->FileOffset.QuadPart !=
803                      (entry->FileOffset.QuadPart + entry->Size))
804                 {
805                     //
806                     // Gap
807                     //
808                     try_return (retVal = FALSE);
809                 }
810
811                 entry = newEntry;
812             }
813         }
814         __except( AFSExceptionFilter( __FUNCTION__, GetExceptionCode(), GetExceptionInformation()) )
815         {
816
817             AFSDbgLogMsg( 0,
818                           0,
819                           "EXCEPTION - AFSDoExtentsMapRegion\n");
820
821             AFSDumpTraceFilesFnc();
822         }
823
824 try_exit:
825
826         *LastExtent = entry;
827     }
828
829     return retVal;
830 }
831
832 NTSTATUS
833 AFSRequestExtentsAsync( IN AFSFcb *Fcb,
834                         IN AFSCcb *Ccb,
835                         IN PLARGE_INTEGER Offset,
836                         IN ULONG Size)
837 {
838
839     AFSDeviceExt        *pDevExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
840     NTSTATUS             ntStatus = STATUS_SUCCESS;
841     AFSExtent           *pExtent = NULL;
842     AFSRequestExtentsCB  request;
843     AFSNonPagedFcb      *pNPFcb = Fcb->NPFcb;
844     AFSExtent           *pFirstExtent = NULL;
845     LARGE_INTEGER        liAlignedOffset;
846     ULONG                ulAlignedLength = 0;
847     BOOLEAN              bRegionMapped = FALSE;
848     ULONGLONG            ullProcessId = (ULONGLONG)PsGetCurrentProcessId();
849
850     __Enter
851     {
852
853         ASSERT( !ExIsResourceAcquiredLite( &pNPFcb->Specific.File.ExtentsResource ));
854
855         //
856         // If the service set a failure on the file since the last
857         // CreateFile was issued, return it now.
858         //
859
860         if (!NT_SUCCESS( pNPFcb->Specific.File.ExtentsRequestStatus))
861         {
862
863             //
864             // If this isn't the same authgroup which caused the failure
865             // then try to request them again
866             //
867
868             if( RtlCompareMemory( &pNPFcb->Specific.File.ExtentsRequestAuthGroup,
869                                   &Ccb->AuthGroup,
870                                   sizeof( GUID)) == sizeof( GUID))
871             {
872
873                 ntStatus = pNPFcb->Specific.File.ExtentsRequestStatus;
874
875                 pNPFcb->Specific.File.ExtentsRequestStatus = STATUS_SUCCESS;
876
877                 RtlZeroMemory( &pNPFcb->Specific.File.ExtentsRequestAuthGroup,
878                                sizeof( GUID));
879
880                 try_return( ntStatus);
881             }
882         }
883
884         //
885         // Check if we are already mapped
886         //
887
888         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
889                       AFS_TRACE_LEVEL_VERBOSE,
890                       "AFSRequestExtentsAsync Acquiring Fcb extents lock %08lX SHARED %08lX\n",
891                       &pNPFcb->Specific.File.ExtentsResource,
892                       PsGetCurrentThread());
893
894         AFSAcquireShared( &pNPFcb->Specific.File.ExtentsResource, TRUE );
895
896         bRegionMapped = AFSDoExtentsMapRegion( Fcb, Offset, Size, &pFirstExtent, &pExtent);
897
898         if( bRegionMapped)
899         {
900
901             KeClearEvent( &pNPFcb->Specific.File.ExtentsRequestComplete );
902         }
903
904         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
905                       AFS_TRACE_LEVEL_VERBOSE,
906                       "AFSRequestExtentsAsync Releasing Fcb extents lock %08lX SHARED %08lX\n",
907                       &pNPFcb->Specific.File.ExtentsResource,
908                       PsGetCurrentThread());
909
910         AFSReleaseResource( &pNPFcb->Specific.File.ExtentsResource );
911
912         if ( bRegionMapped)
913         {
914
915             try_return( ntStatus = STATUS_SUCCESS);
916         }
917
918         //
919         // Align our request on extent size boundary
920         //
921
922         ulAlignedLength = Size;
923
924         liAlignedOffset = *Offset;
925
926         if( liAlignedOffset.QuadPart % pDevExt->Specific.RDR.CacheBlockSize != 0)
927         {
928
929             liAlignedOffset.QuadPart = (ULONGLONG)( (ULONGLONG)(liAlignedOffset.QuadPart / pDevExt->Specific.RDR.CacheBlockSize) * (ULONGLONG)pDevExt->Specific.RDR.CacheBlockSize);
930
931             ulAlignedLength += (ULONG)(Offset->QuadPart - liAlignedOffset.QuadPart);
932         }
933
934         if( ulAlignedLength % pDevExt->Specific.RDR.CacheBlockSize != 0)
935         {
936
937             ulAlignedLength = (ULONG)(((ulAlignedLength / pDevExt->Specific.RDR.CacheBlockSize) + 1) * pDevExt->Specific.RDR.CacheBlockSize);
938         }
939
940         RtlZeroMemory( &request,
941                        sizeof( AFSRequestExtentsCB));
942
943         request.ByteOffset = liAlignedOffset;
944         request.Length = ulAlignedLength;
945
946         if( !AFSIsExtentRequestQueued( &Fcb->ObjectInformation->FileId,
947                                        &request.ByteOffset,
948                                        request.Length))
949         {
950
951             KeClearEvent( &pNPFcb->Specific.File.ExtentsRequestComplete );
952
953             AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
954                           AFS_TRACE_LEVEL_VERBOSE,
955                           "AFSRequestExtentsAsync Request extents for fid %08lX-%08lX-%08lX-%08lX Offset %08lX Len %08lX Thread %08lX\n",
956                           Fcb->ObjectInformation->FileId.Cell,
957                           Fcb->ObjectInformation->FileId.Volume,
958                           Fcb->ObjectInformation->FileId.Vnode,
959                           Fcb->ObjectInformation->FileId.Unique,
960                           request.ByteOffset.LowPart,
961                           request.Length,
962                           PsGetCurrentThread());
963
964             ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_REQUEST_FILE_EXTENTS,
965                                           0,
966                                           &Ccb->AuthGroup,
967                                           NULL,
968                                           &Fcb->ObjectInformation->FileId,
969                                           &request,
970                                           sizeof( AFSRequestExtentsCB ),
971                                           NULL,
972                                           NULL);
973
974             if (  ntStatus == STATUS_ACCESS_DENIED)
975             {
976                 GUID                 stAuthGroup;
977                 DWORD                ntStatus2;
978
979                 ntStatus2 = AFSRetrieveValidAuthGroup( Fcb,
980                                                       NULL,
981                                                       TRUE,
982                                                       &stAuthGroup);
983
984                 if ( NT_SUCCESS( ntStatus2) &&
985                      RtlCompareMemory( &stAuthGroup,
986                                        &Ccb->AuthGroup,
987                                        sizeof( GUID)) != sizeof( GUID))
988                 {
989
990                     ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_REQUEST_FILE_EXTENTS,
991                                                   0,
992                                                   &stAuthGroup,
993                                                   NULL,
994                                                   &Fcb->ObjectInformation->FileId,
995                                                   &request,
996                                                   sizeof( AFSRequestExtentsCB ),
997                                                   NULL,
998                                                   NULL);
999                 }
1000             }
1001
1002             if( !NT_SUCCESS( ntStatus))
1003             {
1004
1005                 KeSetEvent( &pNPFcb->Specific.File.ExtentsRequestComplete,
1006                             0,
1007                             FALSE);
1008             }
1009         }
1010
1011 try_exit:
1012
1013         NOTHING;
1014     }
1015
1016     return ntStatus;
1017 }
1018
1019 NTSTATUS
1020 AFSProcessExtentsResult( IN AFSFcb *Fcb,
1021                          IN ULONG   Count,
1022                          IN AFSFileExtentCB *Result)
1023 {
1024     NTSTATUS          ntStatus = STATUS_SUCCESS;
1025     AFSFileExtentCB  *pFileExtents = Result;
1026     AFSExtent        *pExtent;
1027     LIST_ENTRY       *le;
1028     AFSNonPagedFcb   *pNPFcb = Fcb->NPFcb;
1029     ULONG             fileExtentsUsed = 0;
1030     BOOLEAN           bFoundExtent = FALSE;
1031     LIST_ENTRY       *pSkipEntries[AFS_NUM_EXTENT_LISTS] = { 0 };
1032     AFSDeviceExt     *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
1033     LONG              lCount;
1034
1035     //
1036     // Grab the extents exclusive for the duration
1037     //
1038
1039     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1040                   AFS_TRACE_LEVEL_VERBOSE,
1041                   "AFSProcessExtentsResult Acquiring Fcb extent lock %08lX EXCL %08lX\n",
1042                   &pNPFcb->Specific.File.ExtentsResource,
1043                   PsGetCurrentThread());
1044
1045     AFSAcquireExcl( &pNPFcb->Specific.File.ExtentsResource, TRUE );
1046
1047     __Enter
1048     {
1049
1050         //
1051         // Find where to put the extents
1052         //
1053         for (ULONG i = AFS_EXTENTS_LIST; i < AFS_NUM_EXTENT_LISTS; i++)
1054         {
1055
1056             pSkipEntries[i] = Fcb->Specific.File.ExtentsLists[i].Flink;
1057         }
1058
1059         le = pSkipEntries[AFS_EXTENTS_LIST];
1060
1061         if (le == &Fcb->Specific.File.ExtentsLists[AFS_EXTENTS_LIST])
1062         {
1063             //
1064             // No extents.  Insert at head of list (which is where the skip lists point!)
1065             //
1066             pExtent = NULL;
1067         }
1068         else if (0 != pFileExtents->FileOffset.QuadPart)
1069         {
1070             //
1071             // We want to find the best extents immediately *behind* this offset
1072             //
1073             LARGE_INTEGER offset = pFileExtents->FileOffset;
1074
1075             //
1076             // Ask in the top skip list first, then work down
1077             //
1078             for (LONG i = AFS_NUM_EXTENT_LISTS-1; i >= AFS_EXTENTS_LIST; i--)
1079             {
1080                 pExtent = ExtentForOffsetInList( Fcb,
1081                                                  pSkipEntries[i],
1082                                                  i,
1083                                                  &offset);
1084
1085                 if (NULL == pExtent)
1086                 {
1087                     //
1088                     // No dice.  Header has to become the head of the list
1089                     //
1090                     pSkipEntries[i] = &Fcb->Specific.File.ExtentsLists[i];
1091                     //
1092                     // And as  a loop invariant we should never have found an extent
1093                     //
1094                     ASSERT(!bFoundExtent);
1095                 }
1096                 else
1097                 {
1098                     //
1099                     // pExtent is where to start to insert at this level
1100                     //
1101                     pSkipEntries[i] = &pExtent->Lists[i];
1102
1103                     //
1104                     // And also where to start to look at the next level
1105                     //
1106
1107                     if (i > AFS_EXTENTS_LIST)
1108                     {
1109                         pSkipEntries[i-1] = &pExtent->Lists[i-1];
1110                     }
1111                     bFoundExtent = TRUE;
1112                 }
1113             }
1114
1115             if (NULL == pExtent)
1116             {
1117                 pExtent = ExtentFor( le, AFS_EXTENTS_LIST);
1118                 le = le->Blink;
1119             }
1120             else
1121             {
1122                 le = pExtent->Lists[AFS_EXTENTS_LIST].Blink;
1123             }
1124         }
1125         else
1126         {
1127             //
1128             // Looking at offset 0, so we must start at the beginning
1129             //
1130
1131             pExtent = ExtentFor(le, AFS_EXTENTS_LIST);
1132             le = le->Blink;
1133
1134             //
1135             // And set up the skip lists
1136             //
1137
1138             for (ULONG i = AFS_EXTENTS_LIST; i < AFS_NUM_EXTENT_LISTS; i++)
1139             {
1140                 pSkipEntries[i] = &Fcb->Specific.File.ExtentsLists[i];
1141             }
1142         }
1143
1144         while (fileExtentsUsed < Count)
1145         {
1146
1147             //
1148             // Loop invariant - le points to where to insert after and
1149             // pExtent points to le->fLink
1150             //
1151
1152             ASSERT (NULL == pExtent ||
1153                     le->Flink == &pExtent->Lists[AFS_EXTENTS_LIST]);
1154
1155             if (NULL == pExtent ||
1156                 pExtent->FileOffset.QuadPart > pFileExtents->FileOffset.QuadPart)
1157             {
1158                 //
1159                 // We need to insert a new extent at le.  Start with
1160                 // some sanity check on spanning
1161                 //
1162                 if (NULL != pExtent &&
1163                     ((pFileExtents->FileOffset.QuadPart + pFileExtents->Length) >
1164                      pExtent->FileOffset.QuadPart))
1165                 {
1166                     //
1167                     // File Extents overlaps pExtent
1168                     //
1169                     ASSERT( (pFileExtents->FileOffset.QuadPart + pFileExtents->Length) <=
1170                             pExtent->FileOffset.QuadPart);
1171
1172                     try_return (ntStatus = STATUS_INVALID_PARAMETER);
1173                 }
1174
1175                 //
1176                 // File offset is entirely in front of this extent.  Create
1177                 // a new one (remember le is the previous list entry)
1178                 //
1179                 pExtent = (AFSExtent *) AFSExAllocatePoolWithTag( NonPagedPool,
1180                                                                   sizeof( AFSExtent),
1181                                                                   AFS_EXTENT_TAG );
1182                 if (NULL  == pExtent)
1183                 {
1184
1185                     try_return (ntStatus = STATUS_INSUFFICIENT_RESOURCES );
1186                 }
1187
1188                 RtlZeroMemory( pExtent, sizeof( AFSExtent ));
1189
1190                 pExtent->FileOffset = pFileExtents->FileOffset;
1191                 pExtent->CacheOffset = pFileExtents->CacheOffset;
1192                 pExtent->Size = pFileExtents->Length;
1193
1194                 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1195                               AFS_TRACE_LEVEL_VERBOSE,
1196                               "AFSProcessExtentsResult Received extent for fid %08lX-%08lX-%08lX-%08lX File Offset %I64X Cache Offset %I64X Len %08lX\n",
1197                               Fcb->ObjectInformation->FileId.Cell,
1198                               Fcb->ObjectInformation->FileId.Volume,
1199                               Fcb->ObjectInformation->FileId.Vnode,
1200                               Fcb->ObjectInformation->FileId.Unique,
1201                               pFileExtents->FileOffset.QuadPart,
1202                               pFileExtents->CacheOffset.QuadPart,
1203                               pFileExtents->Length);
1204
1205                 InterlockedExchangeAdd( &pControlDevExt->Specific.Control.ExtentsHeldLength, (LONG)(pExtent->Size/1024));
1206
1207                 InterlockedExchangeAdd( &Fcb->Specific.File.ExtentLength, (LONG)(pExtent->Size/1024));
1208
1209                 lCount = InterlockedIncrement( &Fcb->Specific.File.ExtentCount);
1210
1211                 lCount = InterlockedIncrement( &pControlDevExt->Specific.Control.ExtentCount);
1212
1213                 if( lCount == 1)
1214                 {
1215
1216                     KeClearEvent( &pControlDevExt->Specific.Control.ExtentsHeldEvent);
1217                 }
1218
1219                 //
1220                 // Insert into list
1221                 //
1222                 InsertHeadList(le, &pExtent->Lists[AFS_EXTENTS_LIST]);
1223                 ASSERT(le->Flink == &pExtent->Lists[AFS_EXTENTS_LIST]);
1224                 ASSERT(0 == (pExtent->FileOffset.LowPart & ExtentsMasks[AFS_EXTENTS_LIST]));
1225
1226                 //
1227                 // Do not move the cursor - we will do it next time
1228                 //
1229
1230                 //
1231                 // And into the (upper) skip lists - Again, do not move the cursor
1232                 //
1233                 for (ULONG i = AFS_NUM_EXTENT_LISTS-1; i > AFS_EXTENTS_LIST; i--)
1234                 {
1235                     if (0 == (pExtent->FileOffset.LowPart & ExtentsMasks[i]))
1236                     {
1237                         InsertHeadList(pSkipEntries[i], &pExtent->Lists[i]);
1238 #if AFS_VALIDATE_EXTENTS
1239                         VerifyExtentsLists(Fcb);
1240 #endif
1241                     }
1242                 }
1243             }
1244             else if (pExtent->FileOffset.QuadPart == pFileExtents->FileOffset.QuadPart)
1245             {
1246
1247                 if (pExtent->Size != pFileExtents->Length)
1248                 {
1249
1250                     ASSERT (pExtent->Size == pFileExtents->Length);
1251
1252                     try_return (ntStatus = STATUS_INVALID_PARAMETER);
1253                 }
1254
1255                 //
1256                 // Move both cursors forward.
1257                 //
1258                 // First the extent pointer
1259                 //
1260                 fileExtentsUsed++;
1261                 le = &pExtent->Lists[AFS_EXTENTS_LIST];
1262
1263                 //
1264                 // Then the skip lists cursors forward if needed
1265                 //
1266                 for (ULONG i = AFS_NUM_EXTENT_LISTS-1; i > AFS_EXTENTS_LIST; i--)
1267                 {
1268                     if (0 == (pExtent->FileOffset.LowPart & ExtentsMasks[i]))
1269                     {
1270                         //
1271                         // Check sanity before
1272                         //
1273 #if AFS_VALIDATE_EXTENTS
1274                         VerifyExtentsLists(Fcb);
1275 #endif
1276
1277                         //
1278                         // Skip list should point to us
1279                         //
1280                         //ASSERT(pSkipEntries[i] == &pExtent->Lists[i]);
1281                         //
1282                         // Move forward cursor
1283                         //
1284                         pSkipEntries[i] = pSkipEntries[i]->Flink;
1285                         //
1286                         // Check sanity before
1287                         //
1288 #if AFS_VALIDATE_EXTENTS
1289                         VerifyExtentsLists(Fcb);
1290 #endif
1291                     }
1292                 }
1293
1294                 //
1295                 // And then the cursor in the supplied array
1296                 //
1297
1298                 pFileExtents++;
1299
1300                 //
1301                 // setup pExtent if there is one
1302                 //
1303                 if (le->Flink != &Fcb->Specific.File.ExtentsLists[AFS_EXTENTS_LIST])
1304                 {
1305                     pExtent = NextExtent( pExtent, AFS_EXTENTS_LIST ) ;
1306                 }
1307                 else
1308                 {
1309                     pExtent = NULL;
1310                 }
1311             }
1312             else
1313             {
1314
1315                 ASSERT( pExtent->FileOffset.QuadPart < pFileExtents->FileOffset.QuadPart );
1316
1317                 //
1318                 // Sanity check on spanning
1319                 //
1320                 if ((pExtent->FileOffset.QuadPart + pExtent->Size) >
1321                     pFileExtents->FileOffset.QuadPart)
1322                 {
1323
1324                     ASSERT( (pExtent->FileOffset.QuadPart + pExtent->Size) <=
1325                             pFileExtents->FileOffset.QuadPart);
1326
1327                     try_return (ntStatus = STATUS_INVALID_PARAMETER);
1328                 }
1329
1330                 //
1331                 // Move le and pExtent forward
1332                 //
1333                 le = &pExtent->Lists[AFS_EXTENTS_LIST];
1334
1335                 //
1336                 // setup pExtent if there is one
1337                 //
1338
1339                 if (le->Flink != &Fcb->Specific.File.ExtentsLists[AFS_EXTENTS_LIST])
1340                 {
1341                     pExtent = NextExtent( pExtent, AFS_EXTENTS_LIST ) ;
1342                 }
1343                 else
1344                 {
1345                     pExtent = NULL;
1346                 }
1347             }
1348         }
1349
1350         //
1351         // All done, signal that we are done drop the lock, exit
1352         //
1353
1354 try_exit:
1355
1356         if( !NT_SUCCESS( ntStatus))
1357         {
1358
1359             //
1360             // If we failed the service is going to drop all extents so trim away the
1361             // set given to us
1362             //
1363
1364             AFSTrimSpecifiedExtents( Fcb,
1365                                      Count,
1366                                      Result);
1367         }
1368
1369 #if AFS_VALIDATE_EXTENTS
1370         VerifyExtentsLists(Fcb);
1371 #endif
1372
1373         KeSetEvent( &pNPFcb->Specific.File.ExtentsRequestComplete,
1374                     0,
1375                     FALSE);
1376
1377         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1378                       AFS_TRACE_LEVEL_VERBOSE,
1379                       "AFSProcessExtentsResult Releasing Fcb extent lock %08lX EXCL %08lX\n",
1380                       &pNPFcb->Specific.File.ExtentsResource,
1381                       PsGetCurrentThread());
1382
1383         AFSReleaseResource( &pNPFcb->Specific.File.ExtentsResource );
1384     }
1385
1386     return ntStatus;
1387 }
1388
1389 NTSTATUS
1390 AFSProcessSetFileExtents( IN AFSSetFileExtentsCB *SetExtents )
1391 {
1392     AFSFcb       *pFcb = NULL;
1393     AFSVolumeCB  *pVolumeCB = NULL;
1394     NTSTATUS      ntStatus = STATUS_SUCCESS;
1395     AFSDeviceExt *pDevExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
1396     ULONGLONG     ullIndex = 0;
1397     AFSObjectInfoCB *pObjectInfo = NULL;
1398     LONG          lCount;
1399
1400     __Enter
1401     {
1402
1403         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1404                       AFS_TRACE_LEVEL_VERBOSE,
1405                       "AFSProcessSetFileExtents Acquiring RDR VolumeTreeLock lock %08lX SHARED %08lX\n",
1406                       &pDevExt->Specific.RDR.VolumeTreeLock,
1407                       PsGetCurrentThread());
1408
1409         AFSAcquireShared( &pDevExt->Specific.RDR.VolumeTreeLock, TRUE);
1410
1411         AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1412                       AFS_TRACE_LEVEL_VERBOSE,
1413                       "AFSProcessSetFileExtents Set extents for fid %08lX-%08lX-%08lX-%08lX\n",
1414                       SetExtents->FileId.Cell,
1415                       SetExtents->FileId.Volume,
1416                       SetExtents->FileId.Vnode,
1417                       SetExtents->FileId.Unique);
1418
1419         //
1420         // Locate the volume node
1421         //
1422
1423         ullIndex = AFSCreateHighIndex( &SetExtents->FileId);
1424
1425         ntStatus = AFSLocateHashEntry( pDevExt->Specific.RDR.VolumeTree.TreeHead,
1426                                        ullIndex,
1427                                        (AFSBTreeEntry **)&pVolumeCB);
1428
1429         if( pVolumeCB != NULL)
1430         {
1431
1432             lCount = InterlockedIncrement( &pVolumeCB->VolumeReferenceCount);
1433
1434             AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
1435                           AFS_TRACE_LEVEL_VERBOSE,
1436                           "AFSProcessSetFileExtents Increment count on volume %08lX Cnt %d\n",
1437                           pVolumeCB,
1438                           lCount);
1439         }
1440
1441         AFSReleaseResource( &pDevExt->Specific.RDR.VolumeTreeLock);
1442
1443         if( !NT_SUCCESS( ntStatus) ||
1444             pVolumeCB == NULL)
1445         {
1446
1447             AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1448                           AFS_TRACE_LEVEL_ERROR,
1449                           "AFSProcessSetFileExtents Set extents for fid %08lX-%08lX-%08lX-%08lX Failed to locate volume Status %08lX\n",
1450                           SetExtents->FileId.Cell,
1451                           SetExtents->FileId.Volume,
1452                           SetExtents->FileId.Vnode,
1453                           SetExtents->FileId.Unique,
1454                           ntStatus);
1455
1456             try_return( ntStatus = STATUS_UNSUCCESSFUL);
1457         }
1458
1459         AFSAcquireShared( pVolumeCB->ObjectInfoTree.TreeLock,
1460                           TRUE);
1461
1462         //
1463         // Now locate the Object in this volume
1464         //
1465
1466         ullIndex = AFSCreateLowIndex( &SetExtents->FileId);
1467
1468         ntStatus = AFSLocateHashEntry( pVolumeCB->ObjectInfoTree.TreeHead,
1469                                        ullIndex,
1470                                        (AFSBTreeEntry **)&pObjectInfo);
1471
1472         if( pObjectInfo != NULL)
1473         {
1474
1475             //
1476             // Reference the node so it won't be torn down
1477             //
1478
1479             lCount = AFSObjectInfoIncrement( pObjectInfo);
1480
1481             AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
1482                           AFS_TRACE_LEVEL_VERBOSE,
1483                           "AFSProcessSetFileExtents Increment count on object %08lX Cnt %d\n",
1484                           pObjectInfo,
1485                           lCount);
1486         }
1487
1488         AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
1489
1490         if( !NT_SUCCESS( ntStatus) ||
1491             pObjectInfo == NULL)
1492         {
1493
1494             AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1495                           AFS_TRACE_LEVEL_ERROR,
1496                           "AFSProcessSetFileExtents Set extents for hash %I64X fid %08lX-%08lX-%08lX-%08lX Failed to locate file in volume %08lX\n",
1497                           ullIndex,
1498                           SetExtents->FileId.Cell,
1499                           SetExtents->FileId.Volume,
1500                           SetExtents->FileId.Vnode,
1501                           SetExtents->FileId.Unique,
1502                           pVolumeCB);
1503
1504             try_return( ntStatus = STATUS_UNSUCCESSFUL);
1505         }
1506
1507         pFcb = pObjectInfo->Fcb;
1508
1509         //
1510         // If we have a result failure then don't bother trying to set the extents
1511         //
1512
1513         if( SetExtents->ResultStatus != STATUS_SUCCESS)
1514         {
1515
1516             AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1517                           AFS_TRACE_LEVEL_ERROR,
1518                           "AFSProcessSetFileExtents Set extents failure fid %08lX-%08lX-%08lX-%08lX ResultStatus %08lX\n",
1519                           SetExtents->FileId.Cell,
1520                           SetExtents->FileId.Volume,
1521                           SetExtents->FileId.Vnode,
1522                           SetExtents->FileId.Unique,
1523                           SetExtents->ResultStatus);
1524
1525             AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1526                           AFS_TRACE_LEVEL_VERBOSE,
1527                           "AFSProcessSetFileExtents Acquiring Fcb extents lock %08lX EXCL %08lX\n",
1528                           &pFcb->NPFcb->Specific.File.ExtentsResource,
1529                           PsGetCurrentThread());
1530
1531             AFSAcquireExcl( &pFcb->NPFcb->Specific.File.ExtentsResource,
1532                             TRUE);
1533
1534             pFcb->NPFcb->Specific.File.ExtentsRequestStatus = STATUS_CANCELLED;
1535
1536             KeSetEvent( &pFcb->NPFcb->Specific.File.ExtentsRequestComplete,
1537                         0,
1538                         FALSE);
1539
1540             AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1541                           AFS_TRACE_LEVEL_VERBOSE,
1542                           "AFSProcessSetFileExtents Releasing Fcb extent lock %08lX EXCL %08lX\n",
1543                           &pFcb->NPFcb->Specific.File.ExtentsResource,
1544                           PsGetCurrentThread());
1545
1546             AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource);
1547
1548             try_return( ntStatus);
1549         }
1550
1551         ntStatus = AFSProcessExtentsResult ( pFcb,
1552                                              SetExtents->ExtentCount,
1553                                              SetExtents->FileExtents );
1554
1555 try_exit:
1556
1557         if( pObjectInfo != NULL)
1558         {
1559
1560             lCount = AFSObjectInfoDecrement( pObjectInfo);
1561
1562             AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
1563                           AFS_TRACE_LEVEL_VERBOSE,
1564                           "AFSProcessSetFileExtents Decrement count on object %08lX Cnt %d\n",
1565                           pObjectInfo,
1566                           lCount);
1567         }
1568
1569         if ( pVolumeCB)
1570         {
1571
1572             lCount = InterlockedDecrement( &pVolumeCB->VolumeReferenceCount);
1573
1574             AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
1575                           AFS_TRACE_LEVEL_VERBOSE,
1576                           "AFSProcessSetFileExtents Decrement count on volume %08lX Cnt %d\n",
1577                           pVolumeCB,
1578                           lCount);
1579         }
1580     }
1581
1582     return ntStatus;
1583 }
1584
1585 //
1586 // Helper fuctions for Usermode initiation of release of extents
1587 //
1588 NTSTATUS
1589 AFSReleaseSpecifiedExtents( IN  AFSReleaseFileExtentsCB *Extents,
1590                             IN  AFSFcb *Fcb,
1591                             OUT AFSFileExtentCB *FileExtents,
1592                             IN  ULONG BufferSize,
1593                             OUT ULONG *ExtentCount,
1594                             OUT BOOLEAN *DirtyExtents)
1595 {
1596     AFSExtent           *pExtent;
1597     LIST_ENTRY          *le;
1598     LIST_ENTRY          *leNext;
1599     ULONG                ulExtentCount = 0;
1600     NTSTATUS             ntStatus = STATUS_SUCCESS;
1601     BOOLEAN              bReleaseAll = FALSE;
1602     AFSDeviceExt        *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
1603     LONG                 lCount;
1604
1605     __Enter
1606     {
1607         ASSERT( ExIsResourceAcquiredExclusiveLite( &Fcb->NPFcb->Specific.File.ExtentsResource));
1608
1609         if (BufferSize < (Extents->ExtentCount * sizeof( AFSFileExtentCB)))
1610         {
1611
1612             AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1613                           AFS_TRACE_LEVEL_VERBOSE,
1614                           "AFSReleaseSpecifiedExtents Buffer too small\n");
1615
1616             try_return( ntStatus = STATUS_BUFFER_TOO_SMALL);
1617         }
1618
1619         RtlZeroMemory( FileExtents, BufferSize);
1620         *ExtentCount = 0;
1621
1622         *DirtyExtents = FALSE;
1623
1624         //
1625         // iterate until we have dealt with all we were asked for or
1626         // are at the end of the list.  Note that this deals (albeit
1627         // badly) with out of order extents
1628         //
1629
1630         pExtent = AFSExtentForOffset( Fcb,
1631                                       &Extents->FileExtents[0].FileOffset,
1632                                       FALSE);
1633
1634         if (NULL == pExtent)
1635         {
1636             le = Fcb->Specific.File.ExtentsLists[AFS_EXTENTS_LIST].Flink;
1637         }
1638         else
1639         {
1640             le = &pExtent->Lists[AFS_EXTENTS_LIST];
1641         }
1642         ulExtentCount = 0;
1643
1644         if( BooleanFlagOn( Extents->Flags, AFS_RELEASE_EXTENTS_FLAGS_RELEASE_ALL) ||
1645             ( Extents->FileId.Cell   == 0 &&
1646               Extents->FileId.Volume == 0 &&
1647               Extents->FileId.Vnode  == 0 &&
1648               Extents->FileId.Unique == 0))
1649         {
1650
1651             bReleaseAll = TRUE;
1652         }
1653
1654         while( le != &Fcb->Specific.File.ExtentsLists[AFS_EXTENTS_LIST] &&
1655                ulExtentCount < Extents->ExtentCount)
1656
1657         {
1658
1659             pExtent = ExtentFor( le, AFS_EXTENTS_LIST);
1660
1661             if( !bReleaseAll)
1662             {
1663
1664                 if( pExtent->FileOffset.QuadPart < Extents->FileExtents[ulExtentCount].FileOffset.QuadPart)
1665                 {
1666                     //
1667                     // Skip forward through the extent list until we get
1668                     // to the one we want
1669                     //
1670                     le = le->Flink;
1671
1672                     continue;
1673                 }
1674                 else if (pExtent->FileOffset.QuadPart > Extents->FileExtents[ulExtentCount].FileOffset.QuadPart)
1675                 {
1676                     //
1677                     // We don't have the extent asked for so return UNKNOWN
1678                     //
1679
1680                     AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1681                                   AFS_TRACE_LEVEL_VERBOSE,
1682                                   "AFSReleaseSpecifiedExtents Located UNKNOWN extent Offset %I64X Len %08lX\n",
1683                                   Extents->FileExtents[ulExtentCount].FileOffset.QuadPart,
1684                                   Extents->FileExtents[ulExtentCount].Length);
1685
1686                     FileExtents[*ExtentCount].Flags = AFS_EXTENT_FLAG_UNKNOWN;
1687
1688                     FileExtents[*ExtentCount].Length = 0;
1689                     FileExtents[*ExtentCount].CacheOffset.QuadPart = 0;
1690                     FileExtents[*ExtentCount].FileOffset = Extents->FileExtents[ulExtentCount].FileOffset;
1691
1692                     *ExtentCount = (*ExtentCount) + 1;
1693
1694                     ulExtentCount++;
1695
1696                     //
1697                     // Reset where we are looking
1698                     //
1699
1700                     le = Fcb->Specific.File.ExtentsLists[AFS_EXTENTS_LIST].Flink;
1701
1702                     continue;
1703                 }
1704                 else if( pExtent->ActiveCount > 0)
1705                 {
1706
1707                     AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1708                                   AFS_TRACE_LEVEL_VERBOSE,
1709                                   "AFSReleaseSpecifiedExtents Located IN_USE extent Offset %I64X Len %08lX\n",
1710                                   Extents->FileExtents[ulExtentCount].FileOffset.QuadPart,
1711                                   Extents->FileExtents[ulExtentCount].Length);
1712
1713                     FileExtents[*ExtentCount].Flags = AFS_EXTENT_FLAG_IN_USE;
1714
1715                     FileExtents[*ExtentCount].Length = 0;
1716                     FileExtents[*ExtentCount].CacheOffset.QuadPart = 0;
1717                     FileExtents[*ExtentCount].FileOffset = Extents->FileExtents[ulExtentCount].FileOffset;
1718
1719                     *ExtentCount = (*ExtentCount) + 1;
1720
1721                     ulExtentCount++;
1722
1723                     //
1724                     // Reset where we are looking
1725                     //
1726
1727                     le = Fcb->Specific.File.ExtentsLists[AFS_EXTENTS_LIST].Flink;
1728
1729                     continue;
1730                 }
1731             }
1732             else
1733             {
1734
1735                 //
1736                 // If the extent is currently active then skip it
1737                 //
1738
1739                 if( pExtent->ActiveCount > 0)
1740                 {
1741
1742                     le = le->Flink;
1743
1744                     continue;
1745                 }
1746             }
1747
1748             FileExtents[*ExtentCount].Flags = AFS_EXTENT_FLAG_RELEASE;
1749
1750             FileExtents[*ExtentCount].Length = pExtent->Size;
1751             FileExtents[*ExtentCount].DirtyLength = pExtent->Size;
1752             FileExtents[*ExtentCount].DirtyOffset = 0;
1753             FileExtents[*ExtentCount].CacheOffset = pExtent->CacheOffset;
1754             FileExtents[*ExtentCount].FileOffset = pExtent->FileOffset;
1755
1756             AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
1757                           AFS_TRACE_LEVEL_VERBOSE,
1758                           "AFSReleaseSpecifiedExtents Releasing extent %p fid %08lX-%08lX-%08lX-%08lX Offset %I64X Len %08lX\n",
1759                           pExtent,
1760                           Fcb->ObjectInformation->FileId.Cell,
1761                           Fcb->ObjectInformation->FileId.Volume,
1762                           Fcb->ObjectInformation->FileId.Vnode,
1763                           Fcb->ObjectInformation->FileId.Unique,
1764                           FileExtents[*ExtentCount].FileOffset.QuadPart,
1765                           FileExtents[*ExtentCount].Length);
1766
1767             if( BooleanFlagOn( pExtent->Flags, AFS_EXTENT_DIRTY))
1768             {
1769
1770                 AFSAcquireExcl( &Fcb->NPFcb->Specific.File.DirtyExtentsListLock,
1771                                 TRUE);
1772
1773                 if( BooleanFlagOn( pExtent->Flags, AFS_EXTENT_DIRTY))
1774                 {
1775
1776                     AFSRemoveEntryDirtyList( Fcb,
1777                                              pExtent);
1778
1779                     FileExtents[*ExtentCount].Flags |= AFS_EXTENT_FLAG_DIRTY;
1780
1781                     lCount = InterlockedDecrement( &Fcb->Specific.File.ExtentsDirtyCount);
1782
1783                     *DirtyExtents = TRUE;
1784                 }
1785
1786                 AFSReleaseResource( &Fcb->NPFcb->Specific.File.DirtyExtentsListLock);
1787             }
1788
1789             //
1790             // move forward all three cursors
1791             //
1792             le = le->Flink;
1793             ulExtentCount ++;
1794             *ExtentCount = (*ExtentCount) + 1;
1795
1796             AFSFreeExtent( Fcb,
1797                            pExtent);
1798         }
1799
1800 try_exit:
1801
1802         NOTHING;
1803     }
1804
1805     return ntStatus;
1806 }
1807
1808 AFSFcb*
1809 AFSFindFcbToClean(ULONG IgnoreTime, AFSFcb *LastFcb, BOOLEAN Block)
1810 {
1811
1812     AFSFcb *pFcb = NULL;
1813     AFSVolumeCB *pVolumeCB = NULL;
1814     AFSDeviceExt *pRDRDeviceExt = NULL;
1815     AFSDeviceExt *pControlDeviceExt = NULL;
1816     BOOLEAN bLocatedEntry = FALSE;
1817     AFSObjectInfoCB *pCurrentObject = NULL;
1818     BOOLEAN bReleaseVolumeListLock = FALSE;
1819     LONG lCount;
1820
1821     pRDRDeviceExt = (AFSDeviceExt *)AFSRDRDeviceObject->DeviceExtension;
1822     pControlDeviceExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
1823
1824     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1825                   AFS_TRACE_LEVEL_VERBOSE,
1826                   "AFSFindFcbToClean Acquiring RDR VolumeListLock lock %08lX SHARED %08lX\n",
1827                   &pRDRDeviceExt->Specific.RDR.VolumeListLock,
1828                   PsGetCurrentThread());
1829
1830     AFSAcquireShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock,
1831                       TRUE);
1832
1833     bReleaseVolumeListLock = TRUE;
1834
1835     pVolumeCB = pRDRDeviceExt->Specific.RDR.VolumeListHead;
1836
1837     while( pVolumeCB != NULL)
1838     {
1839
1840         //
1841         // The Volume list may move under our feet.  Lock it.
1842         //
1843
1844         lCount = InterlockedIncrement( &pVolumeCB->VolumeReferenceCount);
1845
1846         AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
1847                       AFS_TRACE_LEVEL_VERBOSE,
1848                       "AFSFindFcbToClean Increment count on volume %08lX Cnt %d\n",
1849                       pVolumeCB,
1850                       lCount);
1851
1852         AFSReleaseResource( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
1853
1854         bReleaseVolumeListLock = FALSE;
1855
1856         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1857                       AFS_TRACE_LEVEL_VERBOSE,
1858                       "AFSFindFcbToClean Acquiring VolumeRoot ObjectInfoTree lock %08lX SHARED %08lX\n",
1859                       pVolumeCB->ObjectInfoTree.TreeLock,
1860                       PsGetCurrentThread());
1861
1862         AFSAcquireShared( pVolumeCB->ObjectInfoTree.TreeLock,
1863                           TRUE);
1864
1865         lCount = InterlockedDecrement( &pVolumeCB->VolumeReferenceCount);
1866
1867         AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
1868                       AFS_TRACE_LEVEL_VERBOSE,
1869                       "AFSFindFcbToClean Decrement count on volume %08lX Cnt %d\n",
1870                       pVolumeCB,
1871                       lCount);
1872
1873         if( NULL == LastFcb)
1874         {
1875
1876             pCurrentObject = pVolumeCB->ObjectInfoListHead;
1877         }
1878         else
1879         {
1880
1881             pCurrentObject = (AFSObjectInfoCB *)LastFcb->ObjectInformation->ListEntry.fLink;
1882         }
1883
1884         pFcb = NULL;
1885
1886         while( pCurrentObject != NULL)
1887         {
1888
1889             pFcb = (AFSFcb *)pCurrentObject->Fcb;
1890
1891             //
1892             // If the FCB is a candidate we try to lock it (but without waiting - which
1893             // means we are deadlock free
1894             //
1895
1896             if( pFcb != NULL &&
1897                 pFcb->Header.NodeTypeCode == AFS_FILE_FCB)
1898             {
1899
1900                 if( Block)
1901                 {
1902
1903                     AFSLockForExtentsTrim( pFcb);
1904                 }
1905                 else
1906                 {
1907
1908                     if( !AFSLockForExtentsTrimNoWait( pFcb))
1909                     {
1910
1911                         pCurrentObject = (AFSObjectInfoCB *)pCurrentObject->ListEntry.fLink;
1912
1913                         pFcb = NULL;
1914
1915                         continue;
1916                     }
1917                 }
1918
1919                 //
1920                 // Need to be sure there are no current flushes in the queue
1921                 //
1922
1923                 if( pFcb->Specific.File.ExtentCount == 0)
1924                 {
1925
1926                     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1927                                   AFS_TRACE_LEVEL_VERBOSE,
1928                                   "AFSFindFcbToClean Releasing Fcb extent lock %08lX thread %08lX\n",
1929                                   &pFcb->NPFcb->Specific.File.ExtentsResource,
1930                                   PsGetCurrentThread());
1931
1932                     AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource);
1933
1934                     pCurrentObject = (AFSObjectInfoCB *)pCurrentObject->ListEntry.fLink;
1935
1936                     pFcb = NULL;
1937
1938                     continue;
1939                 }
1940
1941                 if( pFcb->Specific.File.QueuedFlushCount > 0)
1942                 {
1943
1944                     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1945                                   AFS_TRACE_LEVEL_VERBOSE,
1946                                   "AFSFindFcbToClean Releasing Fcb extent lock %08lX thread %08lX\n",
1947                                   &pFcb->NPFcb->Specific.File.ExtentsResource,
1948                                   PsGetCurrentThread());
1949
1950                     AFSReleaseResource(&pFcb->NPFcb->Specific.File.ExtentsResource);
1951
1952                     if( Block)
1953                     {
1954                         AFSWaitOnQueuedFlushes( pFcb);
1955                     }
1956                     else
1957                     {
1958
1959                         pCurrentObject = (AFSObjectInfoCB *)pCurrentObject->ListEntry.fLink;
1960                     }
1961
1962                     pFcb = NULL;
1963
1964                     continue;
1965                 }
1966
1967                 if( pFcb->OpenHandleCount > 0)
1968                 {
1969
1970                     AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
1971                                   AFS_TRACE_LEVEL_VERBOSE,
1972                                   "AFSFindFcbToClean Releasing Fcb extent lock %08lX thread %08lX\n",
1973                                   &pFcb->NPFcb->Specific.File.ExtentsResource,
1974                                   PsGetCurrentThread());
1975
1976                     AFSReleaseResource(&pFcb->NPFcb->Specific.File.ExtentsResource);
1977
1978                     pCurrentObject = (AFSObjectInfoCB *)pCurrentObject->ListEntry.fLink;
1979
1980                     pFcb = NULL;
1981
1982                     continue;
1983                 }
1984
1985                 //
1986                 // A hit a very palpable hit.  Pin it
1987                 //
1988
1989                 lCount = AFSObjectInfoIncrement( pCurrentObject);
1990
1991                 AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
1992                               AFS_TRACE_LEVEL_VERBOSE,
1993                               "AFSFindFcbToClean Increment count on Fcb %08lX Cnt %d\n",
1994                               pCurrentObject,
1995                               lCount);
1996
1997                 bLocatedEntry = TRUE;
1998
1999                 break;
2000             }
2001
2002             pCurrentObject = (AFSObjectInfoCB *)pCurrentObject->ListEntry.fLink;
2003
2004             pFcb = NULL;
2005         }
2006
2007         AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
2008
2009         if( bLocatedEntry)
2010         {
2011             break;
2012         }
2013
2014         AFSAcquireShared( &pRDRDeviceExt->Specific.RDR.VolumeListLock,
2015                           TRUE);
2016
2017         bReleaseVolumeListLock = TRUE;
2018
2019         pVolumeCB = (AFSVolumeCB *)pVolumeCB->ListEntry.fLink;
2020     }
2021
2022     if( bReleaseVolumeListLock)
2023     {
2024
2025         AFSReleaseResource( &pRDRDeviceExt->Specific.RDR.VolumeListLock);
2026     }
2027
2028     return pFcb;
2029 }
2030
2031 NTSTATUS
2032 AFSProcessExtentFailure( PIRP Irp)
2033 {
2034     AFSExtentFailureCB                *pFailureCB = NULL;
2035     NTSTATUS                           ntStatus = STATUS_SUCCESS;
2036     AFSDeviceExt                      *pDevExt = (AFSDeviceExt *) AFSRDRDeviceObject->DeviceExtension;
2037     PIO_STACK_LOCATION                 pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2038     AFSVolumeCB                       *pVolumeCB = NULL;
2039     ULONGLONG                          ullIndex = 0;
2040     AFSObjectInfoCB                   *pObjectInfo = NULL;
2041     LONG                               lCount;
2042
2043     __Enter
2044     {
2045         if( pIrpSp->Parameters.DeviceIoControl.InputBufferLength < sizeof( AFSExtentFailureCB))
2046         {
2047
2048             AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
2049                           AFS_TRACE_LEVEL_ERROR,
2050                           "AFSProcessExtentFailure Input buffer too small\n");
2051
2052             try_return( ntStatus = STATUS_INVALID_PARAMETER);
2053         }
2054
2055         pFailureCB = (AFSExtentFailureCB *)Irp->AssociatedIrp.SystemBuffer;
2056
2057         AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
2058                       AFS_TRACE_LEVEL_ERROR,
2059                       "AFSProcessExtentFailure Service Reports Failure fid %08lX-%08lX-%08lX-%08lX Status %08lX\n",
2060                       pFailureCB->FileId.Cell,
2061                       pFailureCB->FileId.Volume,
2062                       pFailureCB->FileId.Vnode,
2063                       pFailureCB->FileId.Unique,
2064                       pFailureCB->FailureStatus);
2065
2066         AFSAcquireShared( &pDevExt->Specific.RDR.VolumeTreeLock, TRUE);
2067
2068         //
2069         // Locate the volume node
2070         //
2071
2072         ullIndex = AFSCreateHighIndex( &pFailureCB->FileId);
2073
2074         ntStatus = AFSLocateHashEntry( pDevExt->Specific.RDR.VolumeTree.TreeHead,
2075                                        ullIndex,
2076                                        (AFSBTreeEntry **)&pVolumeCB);
2077
2078         if( pVolumeCB != NULL)
2079         {
2080
2081             lCount = InterlockedIncrement( &pVolumeCB->VolumeReferenceCount);
2082
2083             AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
2084                           AFS_TRACE_LEVEL_VERBOSE,
2085                           "AFSProcessExtentFailure Increment count on volume %08lX Cnt %d\n",
2086                           pVolumeCB,
2087                           lCount);
2088         }
2089
2090         AFSReleaseResource( &pDevExt->Specific.RDR.VolumeTreeLock);
2091
2092         if( !NT_SUCCESS( ntStatus) ||
2093             pVolumeCB == NULL)
2094         {
2095
2096             AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
2097                           AFS_TRACE_LEVEL_ERROR,
2098                           "AFSProcessExtentFailure Invalid volume index %I64X status %08X\n",
2099                           ullIndex, ntStatus);
2100
2101             try_return( ntStatus = STATUS_UNSUCCESSFUL);
2102         }
2103
2104         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2105                       AFS_TRACE_LEVEL_VERBOSE,
2106                       "AFSProcessExtentFailure Acquiring VolumeRoot FileIDTree.TreeLock lock %08lX SHARED %08lX\n",
2107                       pVolumeCB->ObjectInfoTree.TreeLock,
2108                       PsGetCurrentThread());
2109
2110         AFSAcquireShared( pVolumeCB->ObjectInfoTree.TreeLock,
2111                           TRUE);
2112
2113         //
2114         // Now locate the Object in this volume
2115         //
2116
2117         ullIndex = AFSCreateLowIndex( &pFailureCB->FileId);
2118
2119         ntStatus = AFSLocateHashEntry( pVolumeCB->ObjectInfoTree.TreeHead,
2120                                        ullIndex,
2121                                        (AFSBTreeEntry **)&pObjectInfo);
2122
2123         if( pObjectInfo != NULL &&
2124             pObjectInfo->Fcb != NULL)
2125         {
2126
2127             //
2128             // Reference the node so it won't be torn down
2129             //
2130
2131             lCount = AFSObjectInfoIncrement( pObjectInfo);
2132
2133             AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2134                           AFS_TRACE_LEVEL_VERBOSE,
2135                           "AFSProcessExtentFailure Increment count on object %08lX Cnt %d\n",
2136                           pObjectInfo,
2137                           lCount);
2138         }
2139
2140         AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
2141
2142         if( !NT_SUCCESS( ntStatus) ||
2143             pObjectInfo == NULL ||
2144             pObjectInfo->Fcb == NULL)
2145         {
2146
2147             if( pObjectInfo == NULL)
2148             {
2149                 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
2150                               AFS_TRACE_LEVEL_ERROR,
2151                               "AFSProcessExtentFailure Invalid file index %I64X\n",
2152                               ullIndex);
2153             }
2154             else
2155             {
2156                 AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
2157                               AFS_TRACE_LEVEL_ERROR,
2158                               "AFSProcessExtentFailure Fcb dealocated for %I64X\n",
2159                               ullIndex);
2160             }
2161
2162             try_return( ntStatus = STATUS_UNSUCCESSFUL);
2163         }
2164
2165         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2166                       AFS_TRACE_LEVEL_VERBOSE,
2167                       "AFSProcessExtentFailure Acquiring Fcb extent lock %08lX EXCL %08lX\n",
2168                       &pObjectInfo->Fcb->NPFcb->Specific.File.ExtentsResource,
2169                       PsGetCurrentThread());
2170
2171         AFSAcquireExcl( &pObjectInfo->Fcb->NPFcb->Specific.File.ExtentsResource,
2172                         TRUE);
2173
2174         pObjectInfo->Fcb->NPFcb->Specific.File.ExtentsRequestStatus = pFailureCB->FailureStatus;
2175
2176         RtlCopyMemory( &pObjectInfo->Fcb->NPFcb->Specific.File.ExtentsRequestAuthGroup,
2177                        &pFailureCB->AuthGroup,
2178                        sizeof( GUID));
2179
2180         KeSetEvent( &pObjectInfo->Fcb->NPFcb->Specific.File.ExtentsRequestComplete,
2181                     0,
2182                     FALSE);
2183
2184         AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2185                       AFS_TRACE_LEVEL_VERBOSE,
2186                       "AFSProcessExtentFailure Releasing Fcb extent lock %08lX EXCL %08lX\n",
2187                       &pObjectInfo->Fcb->NPFcb->Specific.File.ExtentsResource,
2188                       PsGetCurrentThread());
2189
2190         AFSReleaseResource( &pObjectInfo->Fcb->NPFcb->Specific.File.ExtentsResource);
2191
2192         lCount = AFSObjectInfoDecrement( pObjectInfo);
2193
2194         AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2195                       AFS_TRACE_LEVEL_VERBOSE,
2196                       "AFSProcessExtentFailure Decrement count on object %08lX Cnt %d\n",
2197                       pObjectInfo,
2198                       lCount);
2199
2200 try_exit:
2201
2202         if ( pVolumeCB)
2203         {
2204
2205             lCount = InterlockedDecrement( &pVolumeCB->VolumeReferenceCount);
2206
2207             AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
2208                           AFS_TRACE_LEVEL_VERBOSE,
2209                           "AFSProcessExtentFailure Decrement count on volume %08lX Cnt %d\n",
2210                           pVolumeCB,
2211                           lCount);
2212         }
2213     }
2214
2215     return ntStatus;
2216 }
2217
2218 NTSTATUS
2219 AFSProcessReleaseFileExtents( IN PIRP Irp)
2220 {
2221     NTSTATUS                           ntStatus = STATUS_SUCCESS;
2222     PIO_STACK_LOCATION                 pIrpSp = IoGetCurrentIrpStackLocation( Irp);
2223     PFILE_OBJECT                       pFileObject = pIrpSp->FileObject;
2224     AFSFcb                            *pFcb = NULL;
2225     AFSVolumeCB                       *pVolumeCB = NULL;
2226     AFSDeviceExt                      *pDevExt;
2227     AFSReleaseFileExtentsCB           *pExtents;
2228     AFSReleaseFileExtentsResultCB     *pResult = NULL;
2229     AFSReleaseFileExtentsResultFileCB *pFile = NULL;
2230     ULONG                              ulSz = 0;
2231     ULONGLONG                          ullIndex = 0;
2232     AFSObjectInfoCB                   *pObjectInfo = NULL;
2233     BOOLEAN                            bLocked = FALSE;
2234     BOOLEAN                            bDirtyExtents = FALSE;
2235     GUID                               stAuthGroup;
2236     LONG                               lCount;
2237
2238     __Enter
2239     {
2240
2241         pDevExt = (AFSDeviceExt *) AFSRDRDeviceObject->DeviceExtension;
2242
2243         pExtents = (AFSReleaseFileExtentsCB*) Irp->AssociatedIrp.SystemBuffer;
2244
2245         if( pIrpSp->Parameters.DeviceIoControl.InputBufferLength <
2246                                             sizeof( AFSReleaseFileExtentsCB))
2247         {
2248
2249             AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
2250                           AFS_TRACE_LEVEL_ERROR,
2251                           "AFSProcessReleaseFileExtents INPUT Buffer too small\n");
2252
2253             try_return( ntStatus = STATUS_INVALID_PARAMETER );
2254         }
2255
2256         if ( pIrpSp->Parameters.DeviceIoControl.OutputBufferLength <
2257                                         sizeof(AFSReleaseFileExtentsResultCB))
2258         {
2259
2260             AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
2261                           AFS_TRACE_LEVEL_ERROR,
2262                           "AFSProcessReleaseFileExtents OUTPUT Buffer too small [1]\n");
2263
2264             //
2265             // Must have space for one extent in one file
2266             //
2267
2268             try_return( ntStatus = STATUS_BUFFER_TOO_SMALL);
2269         }
2270
2271         if (pExtents->ExtentCount == 0)
2272         {
2273
2274             AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
2275                           AFS_TRACE_LEVEL_ERROR,
2276                           "AFSProcessReleaseFileExtents Extent count zero\n");
2277
2278             try_return( ntStatus = STATUS_INVALID_PARAMETER);
2279         }
2280
2281         if (pExtents->FileId.Cell   != 0 ||
2282             pExtents->FileId.Volume != 0 ||
2283             pExtents->FileId.Vnode  != 0 ||
2284             pExtents->FileId.Unique != 0)
2285         {
2286
2287             AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
2288                           AFS_TRACE_LEVEL_VERBOSE,
2289                           "AFSProcessReleaseFileExtents Processing FID %08lX:%08lX:%08lX:%08lX\n",
2290                           pExtents->FileId.Cell,
2291                           pExtents->FileId.Volume,
2292                           pExtents->FileId.Vnode,
2293                           pExtents->FileId.Unique);
2294
2295             if( pIrpSp->Parameters.DeviceIoControl.InputBufferLength <
2296                             ( FIELD_OFFSET( AFSReleaseFileExtentsCB, ExtentCount) + sizeof(ULONG)) ||
2297                 pIrpSp->Parameters.DeviceIoControl.InputBufferLength <
2298                             ( FIELD_OFFSET( AFSReleaseFileExtentsCB, ExtentCount) + sizeof(ULONG) +
2299                                                             sizeof (AFSFileExtentCB) * pExtents->ExtentCount))
2300             {
2301
2302                 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
2303                               AFS_TRACE_LEVEL_ERROR,
2304                               "AFSProcessReleaseFileExtents Buffer too small for FID %08lX:%08lx:%08lX:%08lX\n",
2305                               pExtents->FileId.Cell,
2306                               pExtents->FileId.Volume,
2307                               pExtents->FileId.Vnode,
2308                               pExtents->FileId.Unique);
2309
2310                 try_return( ntStatus = STATUS_INVALID_PARAMETER );
2311             }
2312
2313             AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2314                           AFS_TRACE_LEVEL_VERBOSE,
2315                           "AFSProcessReleaseFileExtents Acquiring RDR VolumeTreeLock lock %08lX SHARED %08lX\n",
2316                           &pDevExt->Specific.RDR.VolumeTreeLock,
2317                           PsGetCurrentThread());
2318
2319             AFSAcquireShared( &pDevExt->Specific.RDR.VolumeTreeLock, TRUE);
2320
2321             //
2322             // Locate the volume node
2323             //
2324
2325             ullIndex = AFSCreateHighIndex( &pExtents->FileId);
2326
2327             ntStatus = AFSLocateHashEntry( pDevExt->Specific.RDR.VolumeTree.TreeHead,
2328                                            ullIndex,
2329                                            (AFSBTreeEntry **)&pVolumeCB);
2330
2331             if( pVolumeCB != NULL)
2332             {
2333
2334                 lCount = InterlockedIncrement( &pVolumeCB->VolumeReferenceCount);
2335
2336                 AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
2337                               AFS_TRACE_LEVEL_VERBOSE,
2338                               "AFSProcessReleaseFileExtents Increment count on volume %08lX Cnt %d\n",
2339                               pVolumeCB,
2340                               lCount);
2341             }
2342
2343             AFSReleaseResource( &pDevExt->Specific.RDR.VolumeTreeLock);
2344
2345             if( !NT_SUCCESS( ntStatus) ||
2346                 pVolumeCB == NULL)
2347             {
2348
2349                 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
2350                               AFS_TRACE_LEVEL_ERROR,
2351                               "AFSProcessReleaseFileExtents Invalid volume index %I64X status %08X\n",
2352                               ullIndex, ntStatus);
2353
2354                 try_return( ntStatus = STATUS_UNSUCCESSFUL);
2355             }
2356
2357             AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2358                           AFS_TRACE_LEVEL_VERBOSE,
2359                           "AFSProcessReleaseFileExtents Acquiring VolumeRoot FileIDTree.TreeLock lock %08lX SHARED %08lX\n",
2360                           pVolumeCB->ObjectInfoTree.TreeLock,
2361                           PsGetCurrentThread());
2362
2363             AFSAcquireShared( pVolumeCB->ObjectInfoTree.TreeLock,
2364                               TRUE);
2365
2366             //
2367             // Now locate the Object in this volume
2368             //
2369
2370             ullIndex = AFSCreateLowIndex( &pExtents->FileId);
2371
2372             ntStatus = AFSLocateHashEntry( pVolumeCB->ObjectInfoTree.TreeHead,
2373                                            ullIndex,
2374                                            (AFSBTreeEntry **)&pObjectInfo);
2375
2376             if( pObjectInfo != NULL)
2377             {
2378
2379                 //
2380                 // Reference the node so it won't be torn down
2381                 //
2382
2383                 lCount = AFSObjectInfoIncrement( pObjectInfo);
2384
2385                 AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2386                               AFS_TRACE_LEVEL_VERBOSE,
2387                               "AFSProcessReleaseFileExtents Increment count on object %08lX Cnt %d\n",
2388                               pObjectInfo,
2389                               lCount);
2390             }
2391
2392             AFSReleaseResource( pVolumeCB->ObjectInfoTree.TreeLock);
2393
2394             if( !NT_SUCCESS( ntStatus) ||
2395                 pObjectInfo == NULL)
2396             {
2397
2398                 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
2399                               AFS_TRACE_LEVEL_ERROR,
2400                               "AFSProcessReleaseFileExtents Invalid file index %I64X\n",
2401                               ullIndex);
2402
2403                 try_return( ntStatus = STATUS_UNSUCCESSFUL);
2404             }
2405
2406             pFcb = pObjectInfo->Fcb;
2407
2408             if( pFcb == NULL)
2409             {
2410
2411                 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
2412                               AFS_TRACE_LEVEL_ERROR,
2413                               "AFSProcessReleaseFileExtents Fcb not initialied (NO EXTENTS) for FID %08lX:%08lx:%08lX:%08lX\n",
2414                               pExtents->FileId.Cell,
2415                               pExtents->FileId.Volume,
2416                               pExtents->FileId.Vnode,
2417                               pExtents->FileId.Unique);
2418
2419                 try_return( ntStatus = STATUS_UNSUCCESSFUL);
2420             }
2421
2422             AFSLockForExtentsTrim( pFcb );
2423
2424             bLocked = TRUE;
2425         }
2426         else
2427         {
2428
2429             //
2430             // Locate an Fcb to trim down
2431             //
2432
2433             AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
2434                           AFS_TRACE_LEVEL_VERBOSE,
2435                           "AFSProcessReleaseFileExtents Searching for a Fcb to Trim Down\n");
2436
2437             pFcb = AFSFindFcbToClean( 0, NULL, FALSE);
2438
2439             if( pFcb == NULL)
2440             {
2441
2442                 pFcb = AFSFindFcbToClean( 0, NULL, TRUE);
2443             }
2444
2445             if( pFcb == NULL)
2446             {
2447
2448                 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
2449                               AFS_TRACE_LEVEL_ERROR,
2450                               "AFSProcessReleaseFileExtents Failed to locate Fcb for release ...\n");
2451
2452                 try_return( ntStatus = STATUS_UNSUCCESSFUL);
2453             }
2454
2455             pObjectInfo = pFcb->ObjectInformation;
2456
2457             bLocked = TRUE;
2458         }
2459
2460         //
2461         // Allocate a scratch buffer to move in the extent information
2462         //
2463
2464         ulSz = (pExtents->ExtentCount-1) * sizeof(AFSFileExtentCB);
2465         ulSz += sizeof(AFSReleaseFileExtentsResultCB);
2466
2467         if (ulSz > pIrpSp->Parameters.DeviceIoControl.OutputBufferLength)
2468         {
2469             AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
2470                           AFS_TRACE_LEVEL_ERROR,
2471                           "AFSProcessReleaseFileExtents OUTPUT Buffer too small [2]\n");
2472
2473             try_return( ntStatus = STATUS_BUFFER_TOO_SMALL );
2474         }
2475
2476         pResult = (AFSReleaseFileExtentsResultCB*) AFSExAllocatePoolWithTag( PagedPool,
2477                                                                              ulSz,
2478                                                                              AFS_EXTENTS_RESULT_TAG);
2479         if (NULL == pResult)
2480         {
2481
2482             AFSDbgLogMsg( AFS_SUBSYSTEM_IO_PROCESSING,
2483                           AFS_TRACE_LEVEL_ERROR,
2484                           "AFSProcessReleaseFileExtents Failed to allocate result block\n");
2485
2486             try_return( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
2487         }
2488
2489         //
2490         // Set up the header (for an array of one)
2491         //
2492         pResult->FileCount = 1;
2493         pResult->Flags = AFS_EXTENT_FLAG_RELEASE;
2494         ulSz -= FIELD_OFFSET(AFSReleaseFileExtentsResultCB, Files);
2495
2496         //
2497         // Setup the first (and only) file
2498         //
2499         pFile = pResult->Files;
2500         pFile->FileId = pObjectInfo->FileId;
2501         pFile->Flags = AFS_EXTENT_FLAG_RELEASE;
2502
2503         //
2504         // Stash away the auth group
2505         //
2506
2507         RtlZeroMemory( &stAuthGroup,
2508                        sizeof( GUID));
2509
2510         ntStatus = AFSRetrieveValidAuthGroup( pFcb,
2511                                               NULL,
2512                                               TRUE,
2513                                               &stAuthGroup);
2514
2515         if( !NT_SUCCESS( ntStatus))
2516         {
2517             try_return( ntStatus);
2518         }
2519
2520         RtlCopyMemory( &pFile->AuthGroup,
2521                        &stAuthGroup,
2522                        sizeof( GUID));
2523
2524         //
2525         // Update the metadata for this call
2526         //
2527
2528         pFile->AllocationSize = pFcb->ObjectInformation->EndOfFile;
2529         pFile->CreateTime = pFcb->ObjectInformation->CreationTime;
2530         pFile->ChangeTime = pFcb->ObjectInformation->ChangeTime;
2531         pFile->LastAccessTime = pFcb->ObjectInformation->LastAccessTime;
2532         pFile->LastWriteTime = pFcb->ObjectInformation->LastWriteTime;
2533
2534         ulSz -= FIELD_OFFSET(AFSReleaseFileExtentsResultFileCB, FileExtents);
2535
2536         ntStatus = AFSReleaseSpecifiedExtents( pExtents,
2537                                                pFcb,
2538                                                pFile->FileExtents,
2539                                                ulSz,
2540                                                &pFile->ExtentCount,
2541                                                &bDirtyExtents);
2542
2543         if (!NT_SUCCESS(ntStatus))
2544         {
2545
2546             AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
2547                           AFS_TRACE_LEVEL_ERROR,
2548                           "AFSProcessReleaseFileExtents Failed to release extents Status %08lX\n",
2549                           ntStatus);
2550
2551             try_return( ntStatus );
2552         }
2553
2554         if( pExtents->ExtentCount == 0)
2555         {
2556
2557             AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
2558                           AFS_TRACE_LEVEL_WARNING,
2559                           "AFSProcessReleaseFileExtents Failed to release ANY extents\n");
2560         }
2561
2562         ulSz = sizeof(AFSReleaseFileExtentsResultCB);
2563
2564         if( pExtents->ExtentCount > 0)
2565         {
2566             ulSz += ((pExtents->ExtentCount-1) * sizeof(AFSFileExtentCB));
2567         }
2568
2569         RtlCopyMemory( Irp->AssociatedIrp.SystemBuffer,
2570                        pResult,
2571                        ulSz);
2572
2573 try_exit:
2574
2575         if( bLocked)
2576         {
2577
2578             AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2579                           AFS_TRACE_LEVEL_VERBOSE,
2580                           "AFSProcessReleaseFileExtents Releasing Fcb extent lock %08lX thread %08lX\n",
2581                           &pFcb->NPFcb->Specific.File.ExtentsResource,
2582                           PsGetCurrentThread());
2583
2584             AFSReleaseResource( &pFcb->NPFcb->Specific.File.ExtentsResource );
2585         }
2586
2587         if( NULL != pResult &&
2588             Irp->AssociatedIrp.SystemBuffer != pResult)
2589         {
2590
2591             AFSExFreePoolWithTag(pResult, AFS_EXTENTS_RESULT_TAG);
2592         }
2593
2594         if (NT_SUCCESS(ntStatus))
2595         {
2596             Irp->IoStatus.Information = ulSz;
2597         }
2598         else
2599         {
2600             Irp->IoStatus.Information = 0;
2601         }
2602
2603         Irp->IoStatus.Status = ntStatus;
2604
2605         if( pObjectInfo != NULL)
2606         {
2607
2608             lCount = AFSObjectInfoDecrement( pObjectInfo);
2609
2610             AFSDbgLogMsg( AFS_SUBSYSTEM_OBJECT_REF_COUNTING,
2611                           AFS_TRACE_LEVEL_VERBOSE,
2612                           "AFSProcessReleaseFileExtents Decrement count on object %08lX Cnt %d\n",
2613                           pObjectInfo,
2614                           lCount);
2615         }
2616
2617         if ( pVolumeCB)
2618         {
2619
2620             lCount = InterlockedDecrement( &pVolumeCB->VolumeReferenceCount);
2621
2622             AFSDbgLogMsg( AFS_SUBSYSTEM_VOLUME_REF_COUNTING,
2623                           AFS_TRACE_LEVEL_VERBOSE,
2624                           "AFSProcessReleaseFileExtents Decrement count on volume %08lX Cnt %d\n",
2625                           pVolumeCB,
2626                           lCount);
2627
2628         }
2629     }
2630
2631     return ntStatus;
2632 }
2633
2634 NTSTATUS
2635 AFSWaitForExtentMapping( AFSFcb *Fcb,
2636                          AFSCcb *Ccb)
2637 {
2638     NTSTATUS ntStatus = STATUS_SUCCESS;
2639     LARGE_INTEGER liTimeOut;
2640     ULONGLONG            ullProcessId = (ULONGLONG)PsGetCurrentProcessId();
2641
2642     __Enter
2643     {
2644
2645         ASSERT( !ExIsResourceAcquiredLite( &Fcb->NPFcb->Specific.File.ExtentsResource ));
2646
2647         if (!NT_SUCCESS( Fcb->NPFcb->Specific.File.ExtentsRequestStatus))
2648         {
2649
2650             //
2651             // If this isn't the same authgroup which caused the failure
2652             // then try to request them again
2653             //
2654
2655             if( RtlCompareMemory( &Fcb->NPFcb->Specific.File.ExtentsRequestAuthGroup,
2656                                   &Ccb->AuthGroup,
2657                                   sizeof( GUID)) == sizeof( GUID))
2658             {
2659
2660                 ntStatus = Fcb->NPFcb->Specific.File.ExtentsRequestStatus;
2661
2662                 Fcb->NPFcb->Specific.File.ExtentsRequestStatus = STATUS_SUCCESS;
2663
2664                 RtlZeroMemory( &Fcb->NPFcb->Specific.File.ExtentsRequestAuthGroup,
2665                                sizeof( GUID));
2666
2667                 try_return( ntStatus);
2668             }
2669         }
2670
2671         liTimeOut.QuadPart = -(1 * AFS_ONE_SECOND);
2672
2673         ntStatus = KeWaitForSingleObject( &Fcb->NPFcb->Specific.File.ExtentsRequestComplete,
2674                                           Executive,
2675                                           KernelMode,
2676                                           FALSE,
2677                                           &liTimeOut);
2678
2679         if (!NT_SUCCESS( Fcb->NPFcb->Specific.File.ExtentsRequestStatus))
2680         {
2681
2682             //
2683             // If this isn't the same authgroup which caused the failure
2684             // or the System Process,
2685             // then try to request the extents again
2686             //
2687
2688             if( RtlCompareMemory( &Fcb->NPFcb->Specific.File.ExtentsRequestAuthGroup,
2689                                   &Ccb->AuthGroup,
2690                                   sizeof( GUID)) == sizeof( GUID) ||
2691                 ullProcessId == (ULONGLONG)AFSSysProcess)
2692             {
2693
2694                 ntStatus = Fcb->NPFcb->Specific.File.ExtentsRequestStatus;
2695
2696                 Fcb->NPFcb->Specific.File.ExtentsRequestStatus = STATUS_SUCCESS;
2697
2698                 RtlZeroMemory( &Fcb->NPFcb->Specific.File.ExtentsRequestAuthGroup,
2699                                sizeof( GUID));
2700
2701                 try_return( ntStatus);
2702             }
2703         }
2704
2705         if( ntStatus == STATUS_TIMEOUT)
2706         {
2707
2708             ntStatus = STATUS_SUCCESS;
2709         }
2710
2711 try_exit:
2712
2713         NOTHING;
2714     }
2715
2716     return ntStatus;
2717 }
2718
2719 NTSTATUS
2720 AFSFlushExtents( IN AFSFcb *Fcb,
2721                  IN GUID *AuthGroup)
2722 {
2723     AFSNonPagedFcb      *pNPFcb = Fcb->NPFcb;
2724     AFSExtent           *pExtent, *pNextExtent;
2725     LIST_ENTRY          *le;
2726     AFSReleaseExtentsCB *pRelease = NULL;
2727     ULONG                count = 0;
2728     ULONG                initialDirtyCount = 0;
2729     BOOLEAN              bExtentsLocked = FALSE;
2730     ULONG                total = 0;
2731     ULONG                sz = 0;
2732     NTSTATUS             ntStatus = STATUS_SUCCESS;
2733     LARGE_INTEGER        liLastFlush;
2734     AFSExtent           *pDirtyListHead = NULL, *pDirtyListTail = NULL;
2735     AFSDeviceExt        *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
2736     GUID                *pAuthGroup = AuthGroup;
2737     GUID                 stAuthGroup;
2738     LONG                 lCount;
2739
2740     ASSERT( Fcb->Header.NodeTypeCode == AFS_FILE_FCB);
2741
2742     //
2743     // Save, then reset the flush time
2744     //
2745
2746     liLastFlush = Fcb->Specific.File.LastServerFlush;
2747
2748     KeQueryTickCount( &Fcb->Specific.File.LastServerFlush);
2749
2750     __Enter
2751     {
2752
2753         lCount = InterlockedIncrement( &Fcb->Specific.File.QueuedFlushCount);
2754
2755         if( pAuthGroup == NULL ||
2756             RtlCompareMemory( pAuthGroup,
2757                               &Fcb->NPFcb->Specific.File.ExtentsRequestAuthGroup,
2758                               sizeof( GUID)) == sizeof( GUID))
2759         {
2760
2761             RtlZeroMemory( &stAuthGroup,
2762                            sizeof( GUID));
2763
2764             ntStatus = AFSRetrieveValidAuthGroup( Fcb,
2765                                                   NULL,
2766                                                   TRUE,
2767                                                   &stAuthGroup);
2768
2769             if( !NT_SUCCESS( ntStatus))
2770             {
2771                 try_return( ntStatus);
2772             }
2773
2774             pAuthGroup = &stAuthGroup;
2775         }
2776
2777         //
2778         // Lock extents while we count and set up the array to send to
2779         // the service
2780         //
2781
2782         AFSLockForExtentsTrim( Fcb);
2783
2784         bExtentsLocked = TRUE;
2785
2786         //
2787         // Clear our queued flush event
2788         //
2789
2790         KeClearEvent( &Fcb->NPFcb->Specific.File.QueuedFlushEvent);
2791
2792         //
2793         // Look for a start in the list to flush entries
2794         //
2795
2796         total = count;
2797
2798         sz = sizeof( AFSReleaseExtentsCB ) + (AFS_MAXIMUM_EXTENT_RELEASE_COUNT * sizeof ( AFSFileExtentCB ));
2799
2800         pRelease = (AFSReleaseExtentsCB*) AFSExAllocatePoolWithTag( NonPagedPool,
2801                                                                     sz,
2802                                                                     AFS_EXTENT_RELEASE_TAG);
2803         if( NULL == pRelease)
2804         {
2805
2806             try_return ( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
2807         }
2808
2809         initialDirtyCount = Fcb->Specific.File.ExtentsDirtyCount;
2810
2811         while( Fcb->Specific.File.ExtentsDirtyCount > 0)
2812         {
2813
2814             pRelease->Flags = AFS_EXTENT_FLAG_DIRTY;
2815
2816             if( BooleanFlagOn( Fcb->Flags, AFS_FCB_FILE_CLOSED))
2817             {
2818
2819                 pRelease->Flags |= AFS_EXTENT_FLAG_FLUSH;
2820             }
2821
2822             //
2823             // Update the metadata for this call
2824             //
2825
2826             pRelease->AllocationSize = Fcb->ObjectInformation->EndOfFile;
2827             pRelease->CreateTime = Fcb->ObjectInformation->CreationTime;
2828             pRelease->ChangeTime = Fcb->ObjectInformation->ChangeTime;
2829             pRelease->LastAccessTime = Fcb->ObjectInformation->LastAccessTime;
2830             pRelease->LastWriteTime = Fcb->ObjectInformation->LastWriteTime;
2831
2832             count = 0;
2833
2834             AFSAcquireExcl( &pNPFcb->Specific.File.DirtyExtentsListLock,
2835                             TRUE);
2836
2837             pExtent = (AFSExtent *)pNPFcb->Specific.File.DirtyListHead;
2838
2839             while( count < AFS_MAXIMUM_EXTENT_RELEASE_COUNT)
2840             {
2841
2842                 if ( pExtent == NULL)
2843                 {
2844
2845                     break;
2846                 }
2847
2848                 pNextExtent = (AFSExtent *)pExtent->DirtyList.fLink;
2849
2850                 if ( pExtent->ActiveCount > 0)
2851                 {
2852                     pExtent = pNextExtent;
2853                     continue;
2854                 }
2855
2856                 AFSRemoveEntryDirtyList( Fcb, pExtent);
2857
2858                 pExtent->DirtyList.fLink = NULL;
2859                 pExtent->DirtyList.bLink = NULL;
2860
2861                 lCount = InterlockedDecrement( &Fcb->Specific.File.ExtentsDirtyCount);
2862
2863                 //
2864                 // Clear the flag in advance of the write. If we do
2865                 // things this was we know that the clear is
2866                 // pessimistic (any write which happens from now on
2867                 // will set the flag dirty again).
2868                 //
2869
2870                 pExtent->Flags &= ~AFS_EXTENT_DIRTY;
2871
2872                 pRelease->FileExtents[count].Flags = AFS_EXTENT_FLAG_DIRTY;
2873
2874                 pRelease->FileExtents[count].Length = pExtent->Size;
2875                 pRelease->FileExtents[count].DirtyLength = pExtent->Size;
2876                 pRelease->FileExtents[count].DirtyOffset = 0;
2877                 pRelease->FileExtents[count].CacheOffset = pExtent->CacheOffset;
2878                 pRelease->FileExtents[count].FileOffset = pExtent->FileOffset;
2879
2880 #if GEN_MD5
2881                 RtlCopyMemory( pRelease->FileExtents[count].MD5,
2882                                pExtent->MD5,
2883                                sizeof(pExtent->MD5));
2884
2885                 pRelease->FileExtents[count].Flags |= AFS_EXTENT_FLAG_MD5_SET;
2886 #endif
2887
2888                 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
2889                               AFS_TRACE_LEVEL_VERBOSE,
2890                               "AFSFlushExtents Releasing DIRTY extent %p fid %08lX-%08lX-%08lX-%08lX Offset %I64X Len %08lX\n",
2891                               pExtent,
2892                               Fcb->ObjectInformation->FileId.Cell,
2893                               Fcb->ObjectInformation->FileId.Volume,
2894                               Fcb->ObjectInformation->FileId.Vnode,
2895                               Fcb->ObjectInformation->FileId.Unique,
2896                               pExtent->FileOffset.QuadPart,
2897                               pExtent->Size);
2898
2899                 pRelease->FileExtents[count].Flags |= AFS_EXTENT_FLAG_RELEASE;
2900
2901                 AFSFreeExtent( Fcb,
2902                                pExtent);
2903
2904                 count ++;
2905
2906                 pExtent = pNextExtent;
2907             }
2908
2909             AFSReleaseResource( &pNPFcb->Specific.File.DirtyExtentsListLock);
2910
2911             //
2912             // If we are done then get out
2913             //
2914
2915             if( count == 0)
2916             {
2917
2918                 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
2919                               AFS_TRACE_LEVEL_VERBOSE,
2920                               "AFSFlushExtents No more dirty extents found\n");
2921
2922                 break;
2923             }
2924
2925             //
2926             // Fire off the request synchronously
2927             //
2928
2929             sz = sizeof( AFSReleaseExtentsCB ) + (count * sizeof ( AFSFileExtentCB ));
2930
2931             pRelease->ExtentCount = count;
2932
2933             AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
2934                           AFS_TRACE_LEVEL_VERBOSE,
2935                           "AFSFlushExtents Releasing(1) Fcb extents lock %08lX SHARED %08lX\n",
2936                           &pNPFcb->Specific.File.ExtentsResource,
2937                           PsGetCurrentThread());
2938
2939             AFSReleaseResource( &pNPFcb->Specific.File.ExtentsResource);
2940             bExtentsLocked = FALSE;
2941
2942             KeSetEvent( &pNPFcb->Specific.File.FlushEvent,
2943                         0,
2944                         FALSE);
2945
2946             ntStatus = AFSProcessRequest( AFS_REQUEST_TYPE_RELEASE_FILE_EXTENTS,
2947                                           AFS_REQUEST_FLAG_SYNCHRONOUS,
2948                                           pAuthGroup,
2949                                           NULL,
2950                                           &Fcb->ObjectInformation->FileId,
2951                                           pRelease,
2952                                           sz,
2953                                           NULL,
2954                                           NULL);
2955
2956             if( !NT_SUCCESS(ntStatus))
2957             {
2958
2959                 //
2960                 // Regardless of whether or not the AFSProcessRequest() succeeded, the extents
2961                 // were released (if AFS_EXTENT_FLAG_RELEASE was set).  Log the error so it is known.
2962                 //
2963
2964                 AFSDbgLogMsg( AFS_SUBSYSTEM_EXTENT_PROCESSING,
2965                               AFS_TRACE_LEVEL_ERROR,
2966                               "AFSFlushExtents AFS_REQUEST_TYPE_RELEASE_FILE_EXTENTS failed fid %08lX-%08lX-%08lX-%08lX Status %08lX\n",
2967                               Fcb->ObjectInformation->FileId.Cell,
2968                               Fcb->ObjectInformation->FileId.Volume,
2969                               Fcb->ObjectInformation->FileId.Vnode,
2970                               Fcb->ObjectInformation->FileId.Unique,
2971                               ntStatus);
2972
2973             }
2974
2975             AFSLockForExtentsTrim( Fcb);
2976
2977             bExtentsLocked = TRUE;
2978         }
2979
2980 try_exit:
2981
2982         lCount = InterlockedDecrement( &Fcb->Specific.File.QueuedFlushCount);
2983
2984         ASSERT( lCount >= 0);
2985
2986         if( lCount == 0)
2987         {
2988
2989             KeSetEvent( &pNPFcb->Specific.File.QueuedFlushEvent,
2990                         0,
2991                         FALSE);
2992         }
2993
2994         KeSetEvent( &pNPFcb->Specific.File.FlushEvent,
2995                     0,
2996                     FALSE);
2997
2998         if (bExtentsLocked)
2999         {
3000
3001             AFSDbgLogMsg( AFS_SUBSYSTEM_LOCK_PROCESSING,
3002                           AFS_TRACE_LEVEL_VERBOSE,
3003                           "AFSFlushExtents Releasing(2) Fcb extents lock %08lX SHARED %08lX\n",
3004                           &pNPFcb->Specific.File.ExtentsResource,
3005                           PsGetCurrentThread());
3006
3007             AFSReleaseResource( &pNPFcb->Specific.File.ExtentsResource );
3008         }
3009
3010         if (pRelease)
3011         {
3012             AFSExFreePoolWithTag( pRelease, AFS_EXTENT_RELEASE_TAG);
3013         }
3014     }
3015
3016     return ntStatus;
3017 }
3018
3019 NTSTATUS
3020 AFSReleaseExtentsWithFlush( IN AFSFcb *Fcb,
3021                             IN GUID *AuthGroup,
3022                             IN BOOLEAN bReleaseAll)
3023 {
3024     AFSNonPagedFcb      *pNPFcb = Fcb->NPFcb;
3025     AFSExtent           *pExtent;
3026     LIST_ENTRY          *le;
3027     AFSReleaseExtentsCB *pRelease = NULL;
3028     ULONG                count = 0;
3029     ULONG                initialDirtyCount = 0;
3030     BOOLEAN              bExtentsLocked = FALSE;
3031     ULONG                total = 0;
3032     ULONG                sz = 0;
3033     NTSTATUS             ntStatus = STATUS_SUCCESS;
3034     LARGE_INTEGER        liLastFlush;
3035     ULONG                ulRemainingExtentLength = 0;
3036     AFSDeviceExt        *pControlDevExt = (AFSDeviceExt *)AFSControlDeviceObject->DeviceExtension;
3037     GUID                *pAuthGroup = AuthGroup;
3038     GUID                 stAuthGroup;
3039     LONG                 lCount;
3040
3041     ASSERT( Fcb->Header.NodeTypeCode == AFS_FILE_FCB);
3042
3043     //
3044     // Save, then reset the flush time
3045     //
3046
3047     liLastFlush = Fcb->Specific.File.LastServerFlush;
3048
3049     KeQueryTickCount( &Fcb->Specific.File.LastServerFlush);
3050
3051     __Enter
3052     {
3053
3054         if( pAuthGroup == NULL ||
3055             RtlCompareMemory( pAuthGroup,
3056                               &Fcb->NPFcb->Specific.File.ExtentsRequestAuthGroup,
3057                               sizeof( GUID)) == sizeof( GUID))
3058         {
3059
3060             RtlZeroMemory( &stAuthGroup,
3061                            sizeof( GUID));
3062
3063             ntStatus = AFSRetrieveValidAuthGroup( Fcb,
3064                                                   NULL,
3065                                                   TRUE,
3066                                                   &stAuthGroup);
3067
3068             if( !NT_SUCCESS( ntStatus))
3069             {
3070                 try_return( ntStatus);
3071             }
3072
3073             pAuthGroup = &stAuthGroup;
3074         }
3075
3076         //
3077         // Look for a start in the list to flush entries
3078         //
3079
3080         total = count;
3081
3082         sz = sizeof( AFSReleaseExtentsCB ) + (AFS_MAXIMUM_EXTENT_RELEASE_COUNT * sizeof ( AFSFileExtentCB ));
3083
3084         pRelease = (AFSReleaseExtentsCB*) AFSExAllocatePoolWithTag( NonPagedPool,
3085                                                                     sz,
3086                                                                     AFS_EXTENT_RELEASE_TAG);
3087         if( NULL == pRelease)
3088         {
3089
3090             try_return ( ntStatus = STATUS_INSUFFICIENT_RESOURCES );
3091         }
3092
3093         if( Fcb->OpenHandleCount > 0 &&
3094             !bReleaseAll)
3095         {
3096
3097             //
3098             // Don't release everything ...
3099             //
3100
3101             ulRemainingExtentLength = 1024;
3102         }
3103
3104         while( Fcb->Specific.File.ExtentLength > (LONG)ulRemainingExtentLength)
3105         {
3106