e10a14eb0b87c12e38f9652727e5898d8447c675
[reactos.git] / reactos / sdk / lib / drivers / rxce / rxce.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2017 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 /*
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS kernel
22 * FILE: sdk/lib/drivers/rxce/rxce.c
23 * PURPOSE: RXCE library
24 * PROGRAMMER: Pierre Schweitzer (pierre@reactos.org)
25 */
26
27 /* INCLUDES *****************************************************************/
28
29 #include <rx.h>
30 #include <pseh/pseh2.h>
31 #include <dfs.h>
32
33 #define NDEBUG
34 #include <debug.h>
35
36 VOID
37 RxAssert(
38 PVOID Assert,
39 PVOID File,
40 ULONG Line,
41 PVOID Message);
42
43 VOID
44 NTAPI
45 RxCreateSrvCallCallBack(
46 IN OUT PMRX_SRVCALL_CALLBACK_CONTEXT Context);
47
48 NTSTATUS
49 RxFinishSrvCallConstruction(
50 PMRX_SRVCALLDOWN_STRUCTURE Calldown);
51
52 VOID
53 NTAPI
54 RxFinishSrvCallConstructionDispatcher(
55 IN PVOID Context);
56
57 NTSTATUS
58 RxInsertWorkQueueItem(
59 PRDBSS_DEVICE_OBJECT pMRxDeviceObject,
60 WORK_QUEUE_TYPE WorkQueueType,
61 PRX_WORK_QUEUE_ITEM WorkQueueItem);
62
63 PVOID
64 RxNewMapUserBuffer(
65 PRX_CONTEXT RxContext);
66
67 VOID
68 NTAPI
69 RxpDestroySrvCall(
70 IN PVOID Context);
71
72 VOID
73 RxpDispatchChangeBufferingStateRequests(
74 PSRV_CALL SrvCall,
75 PSRV_OPEN SrvOpen,
76 PLIST_ENTRY DiscardedRequests);
77
78 VOID
79 NTAPI
80 RxScavengerTimerRoutine(
81 PVOID Context);
82
83 VOID
84 NTAPI
85 RxTimerDispatch(
86 _In_ struct _KDPC *Dpc,
87 _In_opt_ PVOID DeferredContext,
88 _In_opt_ PVOID SystemArgument1,
89 _In_opt_ PVOID SystemArgument2);
90
91 VOID
92 NTAPI
93 RxWorkItemDispatcher(
94 PVOID Context);
95
96 PVOID
97 NTAPI
98 _RxAllocatePoolWithTag(
99 _In_ POOL_TYPE PoolType,
100 _In_ SIZE_T NumberOfBytes,
101 _In_ ULONG Tag);
102
103 VOID
104 NTAPI
105 _RxFreePool(
106 _In_ PVOID Buffer);
107
108 VOID
109 NTAPI
110 _RxFreePoolWithTag(
111 _In_ PVOID Buffer,
112 _In_ ULONG Tag);
113
114 extern ULONG ReadAheadGranularity;
115
116 volatile LONG RxNumberOfActiveFcbs = 0;
117 ULONG SerialNumber = 1;
118 PVOID RxNull = NULL;
119 volatile ULONG RxContextSerialNumberCounter;
120 BOOLEAN RxStopOnLoudCompletion = TRUE;
121 BOOLEAN RxSrvCallConstructionDispatcherActive = FALSE;
122 LIST_ENTRY RxSrvCalldownList;
123 RX_SPIN_LOCK RxStrucSupSpinLock;
124 ULONG RdbssReferenceTracingValue;
125 LARGE_INTEGER RxWorkQueueWaitInterval[RxMaximumWorkQueue];
126 LARGE_INTEGER RxSpinUpDispatcherWaitInterval;
127 RX_DISPATCHER RxDispatcher;
128 RX_WORK_QUEUE_DISPATCHER RxDispatcherWorkQueues;
129 FAST_MUTEX RxLowIoPagingIoSyncMutex;
130 BOOLEAN RxContinueFromAssert = TRUE;
131 ULONG RxExplodePoolTags = 1;
132 LARGE_INTEGER RxTimerInterval;
133 RX_SPIN_LOCK RxTimerLock;
134 LIST_ENTRY RxTimerQueueHead;
135 LIST_ENTRY RxRecurrentWorkItemsList;
136 KDPC RxTimerDpc;
137 KTIMER RxTimer;
138 ULONG RxTimerTickCount;
139 #if DBG
140 BOOLEAN DumpDispatchRoutine = TRUE;
141 #else
142 BOOLEAN DumpDispatchRoutine = FALSE;
143 #endif
144
145 #if RDBSS_ASSERTS
146 #ifdef ASSERT
147 #undef ASSERT
148 #endif
149
150 #define ASSERT(exp) \
151 if (!(exp)) \
152 { \
153 RxAssert(#exp, __FILE__, __LINE__, NULL); \
154 }
155 #endif
156
157 #if RX_POOL_WRAPPER
158 #undef RxAllocatePool
159 #undef RxAllocatePoolWithTag
160 #undef RxFreePool
161
162 #define RxAllocatePool(P, S) _RxAllocatePoolWithTag(P, S, 0)
163 #define RxAllocatePoolWithTag _RxAllocatePoolWithTag
164 #define RxFreePool _RxFreePool
165 #define RxFreePoolWithTag _RxFreePoolWithTag
166 #endif
167
168 /* FUNCTIONS ****************************************************************/
169
170 /*
171 * @implemented
172 */
173 VOID
174 RxAddVirtualNetRootToNetRoot(
175 PNET_ROOT NetRoot,
176 PV_NET_ROOT VNetRoot)
177 {
178 PAGED_CODE();
179
180 DPRINT("RxAddVirtualNetRootToNetRoot(%p, %p)\n", NetRoot, VNetRoot);
181
182 /* Insert in the VNetRoot list - make sure lock is held */
183 ASSERT(RxIsPrefixTableLockExclusive(NetRoot->SrvCall->RxDeviceObject->pRxNetNameTable));
184
185 VNetRoot->pNetRoot = (PMRX_NET_ROOT)NetRoot;
186 ++NetRoot->NumberOfVirtualNetRoots;
187 InsertTailList(&NetRoot->VirtualNetRoots, &VNetRoot->NetRootListEntry);
188 }
189
190 /*
191 * @implemented
192 */
193 PVOID
194 RxAllocateFcbObject(
195 PRDBSS_DEVICE_OBJECT RxDeviceObject,
196 NODE_TYPE_CODE NodeType,
197 POOL_TYPE PoolType,
198 ULONG NameSize,
199 PVOID AlreadyAllocatedObject)
200 {
201 PFCB Fcb;
202 PFOBX Fobx;
203 PSRV_OPEN SrvOpen;
204 PVOID Buffer, PAPNBuffer;
205 PNON_PAGED_FCB NonPagedFcb;
206 PMINIRDR_DISPATCH Dispatch;
207 ULONG NonPagedSize, FobxSize, SrvOpenSize, FcbSize;
208
209 PAGED_CODE();
210
211 Dispatch = RxDeviceObject->Dispatch;
212
213 NonPagedSize = 0;
214 FobxSize = 0;
215 SrvOpenSize = 0;
216 FcbSize = 0;
217
218 Fcb = NULL;
219 Fobx = NULL;
220 SrvOpen = NULL;
221 NonPagedFcb = NULL;
222 PAPNBuffer = NULL;
223
224 /* If we ask for FOBX, just allocate FOBX and its extension if asked */
225 if (NodeType == RDBSS_NTC_FOBX)
226 {
227 FobxSize = sizeof(FOBX);
228 if (BooleanFlagOn(Dispatch->MRxFlags, RDBSS_MANAGE_FOBX_EXTENSION))
229 {
230 FobxSize += QuadAlign(Dispatch->MRxFobxSize);
231 }
232 }
233 /* If we ask for SRV_OPEN, also allocate the "internal" FOBX and the extensions if asked */
234 else if (NodeType == RDBSS_NTC_SRVOPEN || NodeType == RDBSS_NTC_INTERNAL_SRVOPEN)
235 {
236 SrvOpenSize = sizeof(SRV_OPEN);
237 if (BooleanFlagOn(Dispatch->MRxFlags, RDBSS_MANAGE_SRV_OPEN_EXTENSION))
238 {
239 SrvOpenSize += QuadAlign(Dispatch->MRxSrvOpenSize);
240 }
241
242 FobxSize = sizeof(FOBX);
243 if (BooleanFlagOn(Dispatch->MRxFlags, RDBSS_MANAGE_FOBX_EXTENSION))
244 {
245 FobxSize += QuadAlign(Dispatch->MRxFobxSize);
246 }
247 }
248 /* Otherwise, we're asked to allocate a FCB */
249 else
250 {
251 /* So, allocate the FCB and its extension if asked */
252 FcbSize = sizeof(FCB);
253 if (BooleanFlagOn(Dispatch->MRxFlags, RDBSS_MANAGE_FCB_EXTENSION))
254 {
255 FcbSize += QuadAlign(Dispatch->MRxFcbSize);
256 }
257
258 /* If we're asked to allocate from nonpaged, also allocate the NON_PAGED_FCB
259 * Otherwise, it will be allocated later on, specifically
260 */
261 if (PoolType == NonPagedPool)
262 {
263 NonPagedSize = sizeof(NON_PAGED_FCB);
264 }
265
266 /* And if it's not for a rename operation also allcoate the internal SRV_OPEN and FOBX and their extensions */
267 if (NodeType != RDBSS_NTC_OPENTARGETDIR_FCB)
268 {
269 SrvOpenSize = sizeof(SRV_OPEN);
270 if (BooleanFlagOn(Dispatch->MRxFlags, RDBSS_MANAGE_SRV_OPEN_EXTENSION))
271 {
272 SrvOpenSize += QuadAlign(Dispatch->MRxSrvOpenSize);
273 }
274
275 FobxSize = sizeof(FOBX);
276 if (BooleanFlagOn(Dispatch->MRxFlags, RDBSS_MANAGE_FOBX_EXTENSION))
277 {
278 FobxSize += QuadAlign(Dispatch->MRxFobxSize);
279 }
280 }
281 }
282
283 /* If we already have a buffer, go ahead */
284 if (AlreadyAllocatedObject != NULL)
285 {
286 Buffer = AlreadyAllocatedObject;
287 }
288 /* Otherwise, allocate it */
289 else
290 {
291 Buffer = RxAllocatePoolWithTag(PoolType, NameSize + FcbSize + SrvOpenSize + FobxSize + NonPagedSize, RX_FCB_POOLTAG);
292 if (Buffer == NULL)
293 {
294 return NULL;
295 }
296 }
297
298 /* Now, get the pointers - FOBX is easy */
299 if (NodeType == RDBSS_NTC_FOBX)
300 {
301 Fobx = Buffer;
302 }
303 /* SRV_OPEN first, FOBX next */
304 else if (NodeType == RDBSS_NTC_SRVOPEN)
305 {
306 SrvOpen = Buffer;
307 Fobx = Add2Ptr(Buffer, SrvOpenSize);
308 }
309 else if (NodeType == RDBSS_NTC_INTERNAL_SRVOPEN)
310 {
311 SrvOpen = Buffer;
312 }
313 else
314 {
315 /* FCB first, and if needed, SRV_OPEN next, FOBX last */
316 Fcb = Buffer;
317 if (NodeType != RDBSS_NTC_OPENTARGETDIR_FCB)
318 {
319 SrvOpen = Add2Ptr(Buffer, FcbSize);
320 Fobx = Add2Ptr(Buffer, FcbSize + SrvOpenSize);
321 }
322
323 /* If we were not allocated from non paged, allocate the NON_PAGED_FCB now */
324 if (PoolType != NonPagedPool)
325 {
326 NonPagedFcb = RxAllocatePoolWithTag(NonPagedPool, sizeof(NON_PAGED_FCB), RX_NONPAGEDFCB_POOLTAG);
327 if (NonPagedFcb == NULL)
328 {
329 RxFreePoolWithTag(Buffer, RX_FCB_POOLTAG);
330 return NULL;
331 }
332
333 PAPNBuffer = Add2Ptr(Buffer, FcbSize + SrvOpenSize + FobxSize);
334 }
335 /* Otherwise, just point at the right place in what has been allocated previously */
336 else
337 {
338 NonPagedFcb = Add2Ptr(Fobx, FobxSize);
339 PAPNBuffer = Add2Ptr(Fobx, FobxSize + NonPagedSize);
340 }
341 }
342
343 /* If we have allocated a SRV_OPEN, initialize it */
344 if (SrvOpen != NULL)
345 {
346 ZeroAndInitializeNodeType(SrvOpen, RDBSS_NTC_SRVOPEN, SrvOpenSize);
347
348 if (NodeType == RDBSS_NTC_SRVOPEN)
349 {
350 SrvOpen->InternalFobx = Fobx;
351 }
352 else
353 {
354 SrvOpen->InternalFobx = NULL;
355 SrvOpen->Flags |= SRVOPEN_FLAG_FOBX_USED;
356 }
357
358 if (BooleanFlagOn(Dispatch->MRxFlags, RDBSS_MANAGE_SRV_OPEN_EXTENSION))
359 {
360 SrvOpen->Context = Add2Ptr(SrvOpen, sizeof(SRV_OPEN));
361 }
362
363 InitializeListHead(&SrvOpen->SrvOpenQLinks);
364 }
365
366 /* If we have allocated a FOBX, initialize it */
367 if (Fobx != NULL)
368 {
369 ZeroAndInitializeNodeType(Fobx, RDBSS_NTC_FOBX, FobxSize);
370
371 if (BooleanFlagOn(Dispatch->MRxFlags, RDBSS_MANAGE_FOBX_EXTENSION))
372 {
373 Fobx->Context = Add2Ptr(Fobx, sizeof(FOBX));
374 }
375 }
376
377 /* If we have allocated a FCB, initialize it */
378 if (Fcb != NULL)
379 {
380 ZeroAndInitializeNodeType(Fcb, RDBSS_STORAGE_NTC(FileTypeNotYetKnown), FcbSize);
381
382 Fcb->NonPaged = NonPagedFcb;
383 ZeroAndInitializeNodeType(Fcb->NonPaged, RDBSS_NTC_NONPAGED_FCB, sizeof(NON_PAGED_FCB));
384 Fcb->CopyOfNonPaged = NonPagedFcb;
385 NonPagedFcb->FcbBackPointer = Fcb;
386
387 Fcb->InternalSrvOpen = SrvOpen;
388 Fcb->InternalFobx = Fobx;
389
390 Fcb->PrivateAlreadyPrefixedName.Length = NameSize;
391 Fcb->PrivateAlreadyPrefixedName.MaximumLength = NameSize;
392 Fcb->PrivateAlreadyPrefixedName.Buffer = PAPNBuffer;
393
394 if (BooleanFlagOn(Dispatch->MRxFlags, RDBSS_MANAGE_FCB_EXTENSION))
395 {
396 Fcb->Context = Add2Ptr(Fcb, sizeof(FCB));
397 }
398
399 ZeroAndInitializeNodeType(&Fcb->FcbTableEntry, RDBSS_NTC_FCB_TABLE_ENTRY, sizeof(RX_FCB_TABLE_ENTRY));
400
401 InterlockedIncrement(&RxNumberOfActiveFcbs);
402 InterlockedIncrement((volatile long *)&RxDeviceObject->NumberOfActiveFcbs);
403
404 ExInitializeFastMutex(&NonPagedFcb->AdvancedFcbHeaderMutex);
405 FsRtlSetupAdvancedHeader(Fcb, &NonPagedFcb->AdvancedFcbHeaderMutex);
406 }
407
408 return Buffer;
409 }
410
411 /*
412 * @implemented
413 */
414 PVOID
415 RxAllocateObject(
416 NODE_TYPE_CODE NodeType,
417 PMINIRDR_DISPATCH MRxDispatch,
418 ULONG NameLength)
419 {
420 ULONG Tag, ObjectSize;
421 PVOID Object, *Extension;
422 PRX_PREFIX_ENTRY PrefixEntry;
423 USHORT StructSize, ExtensionSize;
424
425 PAGED_CODE();
426
427 /* Select the node to allocate and always deal with the fact we may have to manage its extension */
428 ExtensionSize = 0;
429 switch (NodeType)
430 {
431 case RDBSS_NTC_SRVCALL:
432 Tag = RX_SRVCALL_POOLTAG;
433 StructSize = sizeof(SRV_CALL);
434 if (MRxDispatch != NULL && BooleanFlagOn(MRxDispatch->MRxFlags, RDBSS_MANAGE_SRV_CALL_EXTENSION))
435 {
436 ExtensionSize = QuadAlign(MRxDispatch->MRxSrvCallSize);
437 }
438 break;
439
440 case RDBSS_NTC_NETROOT:
441 Tag = RX_NETROOT_POOLTAG;
442 StructSize = sizeof(NET_ROOT);
443 if (BooleanFlagOn(MRxDispatch->MRxFlags, RDBSS_MANAGE_NET_ROOT_EXTENSION))
444 {
445 ExtensionSize = QuadAlign(MRxDispatch->MRxNetRootSize);
446 }
447 break;
448
449 case RDBSS_NTC_V_NETROOT:
450 Tag = RX_V_NETROOT_POOLTAG;
451 StructSize = sizeof(V_NET_ROOT);
452 if (BooleanFlagOn(MRxDispatch->MRxFlags, RDBSS_MANAGE_V_NET_ROOT_EXTENSION))
453 {
454 ExtensionSize = QuadAlign(MRxDispatch->MRxVNetRootSize);
455 }
456 break;
457
458 default:
459 ASSERT(FALSE);
460 break;
461 }
462
463 /* Now, allocate the object */
464 ObjectSize = ExtensionSize + StructSize + NameLength;
465 Object = RxAllocatePoolWithTag(NonPagedPool, ObjectSize, Tag);
466 if (Object == NULL)
467 {
468 return NULL;
469 }
470 /* Initialize it */
471 ZeroAndInitializeNodeType(Object, NodeType, ObjectSize);
472
473 /* For SRV_CALL and NETROOT, the name points to the prefix table name */
474 switch (NodeType)
475 {
476 case RDBSS_NTC_SRVCALL:
477 PrefixEntry = &((PSRV_CALL)Object)->PrefixEntry;
478 Extension = &((PSRV_CALL)Object)->Context;
479 ((PSRV_CALL)Object)->pSrvCallName = &PrefixEntry->Prefix;
480 break;
481
482 case RDBSS_NTC_NETROOT:
483 PrefixEntry = &((PNET_ROOT)Object)->PrefixEntry;
484 Extension = &((PNET_ROOT)Object)->Context;
485 ((PNET_ROOT)Object)->pNetRootName = &PrefixEntry->Prefix;
486 break;
487
488 case RDBSS_NTC_V_NETROOT:
489 PrefixEntry = &((PV_NET_ROOT)Object)->PrefixEntry;
490 Extension = &((PV_NET_ROOT)Object)->Context;
491 break;
492
493 default:
494 ASSERT(FALSE);
495 break;
496 }
497
498 /* Set the prefix table unicode string */
499 RtlZeroMemory(PrefixEntry, sizeof(RX_PREFIX_ENTRY));
500 PrefixEntry->NodeTypeCode = RDBSS_NTC_PREFIX_ENTRY;
501 PrefixEntry->NodeByteSize = sizeof(RX_PREFIX_ENTRY);
502 PrefixEntry->Prefix.Length = NameLength;
503 PrefixEntry->Prefix.MaximumLength = NameLength;
504 PrefixEntry->Prefix.Buffer = Add2Ptr(Object, ExtensionSize + StructSize);
505
506 /* Return the extension if we are asked to manage it */
507 if (ExtensionSize != 0)
508 {
509 *Extension = Add2Ptr(Object, StructSize);
510 }
511
512 return Object;
513 }
514
515 /*
516 * @implemented
517 */
518 VOID
519 RxAssert(
520 PVOID Assert,
521 PVOID File,
522 ULONG Line,
523 PVOID Message)
524 {
525 CHAR Response[2];
526 CONTEXT Context;
527
528 /* If we're not asked to continue, just stop the system */
529 if (!RxContinueFromAssert)
530 {
531 KeBugCheckEx(RDBSS_FILE_SYSTEM, RDBSS_BUG_CHECK_ASSERT | Line, 0, 0, 0);
532 }
533
534 /* Otherwise, capture context to offer the user to dump it */
535 RtlCaptureContext(&Context);
536
537 /* Loop until the user hits 'i' */
538 while (TRUE)
539 {
540 /* If no file provided, use empty name */
541 if (File == NULL)
542 {
543 File = "";
544 }
545
546 /* If no message provided, use empty one */
547 if (Message == NULL)
548 {
549 Message = "";
550 }
551
552 /* Display the message */
553 DbgPrint("\n*** Assertion failed: %s%s\n*** Source File: %s, line %ld\n\n", Message, Assert, File, Line);
554 /* And ask the user */
555 DbgPrompt("Break, Ignore (bi)? ", Response, sizeof(Response));
556 /* If he asks for ignore, quit
557 * In case of invalid input, ask again
558 */
559 if (Response[0] != 'B' && Response[0] != 'b')
560 {
561 if (Response[0] == 'I' || Response[0] == 'i')
562 {
563 return;
564 }
565
566 continue;
567 }
568
569 /* Break: offer the user to dump the context and break */
570 DbgPrint("Execute '!cxr %lx' to dump context\n", &Context);
571 DbgBreakPoint();
572
573 /* Continue looping, so that after dump, execution can continue (with ignore) */
574 }
575 }
576
577 /*
578 * @implemented
579 */
580 VOID
581 NTAPI
582 RxBootstrapWorkerThreadDispatcher(
583 IN PVOID WorkQueue)
584 {
585 PRX_WORK_QUEUE RxWorkQueue;
586
587 PAGED_CODE();
588
589 RxWorkQueue = WorkQueue;
590 RxpWorkerThreadDispatcher(RxWorkQueue, NULL);
591 }
592
593 /*
594 * @implemented
595 */
596 NTSTATUS
597 NTAPI
598 RxChangeBufferingState(
599 PSRV_OPEN SrvOpen,
600 PVOID Context,
601 BOOLEAN ComputeNewState)
602 {
603 PFCB Fcb;
604 NTSTATUS Status, MiniStatus;
605 ULONG NewBufferingState, OldBufferingState;
606
607 PAGED_CODE();
608
609 DPRINT("RxChangeBufferingState(%p, %p, %d)\n", SrvOpen, Context, ComputeNewState);
610
611 Fcb = (PFCB)SrvOpen->pFcb;
612 ASSERT(NodeTypeIsFcb(Fcb));
613 /* First of all, mark that buffering state is changing */
614 SetFlag(Fcb->FcbState, FCB_STATE_BUFFERSTATE_CHANGING);
615
616 /* Assume success */
617 Status = STATUS_SUCCESS;
618 _SEH2_TRY
619 {
620 /* If we're asked to compute a new state, ask the mini-rdr for it */
621 if (ComputeNewState)
622 {
623 MINIRDR_CALL_THROUGH(MiniStatus, Fcb->MRxDispatch, MRxComputeNewBufferingState,
624 ((PMRX_SRV_OPEN)SrvOpen, Context, &NewBufferingState));
625 if (MiniStatus != STATUS_SUCCESS)
626 {
627 NewBufferingState = 0;
628 }
629 }
630 else
631 {
632 /* If not, use SRV_OPEN state */
633 NewBufferingState = SrvOpen->BufferingFlags;
634 }
635
636 /* If no shared access, and if we're not asked to compute a new state, use maximum flags set */
637 if ((Fcb->ShareAccess.SharedRead + Fcb->ShareAccess.SharedWrite + Fcb->ShareAccess.SharedDelete) == 0 && !ComputeNewState)
638 {
639 SetFlag(NewBufferingState, FCB_STATE_BUFFERING_STATE_WITH_NO_SHARES);
640 }
641
642 /* If there's a lock operation to complete, clear that flag */
643 if (Fcb->OutstandingLockOperationsCount != 0)
644 {
645 ClearFlag(NewBufferingState, FCB_STATE_LOCK_BUFFERING_ENABLED);
646 }
647
648 /* Get the old state */
649 OldBufferingState = Fcb->FcbState & FCB_STATE_BUFFERING_STATE_MASK;
650 DPRINT("ChangeBufferingState %x -> %x (%x)\n", OldBufferingState, NewBufferingState, SrvOpen->BufferingFlags);
651
652 /* If we're dropping write cache, then flush the FCB */
653 if (BooleanFlagOn(OldBufferingState, FCB_STATE_WRITECACHING_ENABLED) &&
654 !BooleanFlagOn(NewBufferingState, FCB_STATE_WRITECACHING_ENABLED))
655 {
656 DPRINT("Flushing\n");
657
658 Status = RxFlushFcbInSystemCache(Fcb, TRUE);
659 }
660
661 /* If we're dropping read cache, then purge */
662 if (Fcb->UncleanCount == 0 ||
663 (BooleanFlagOn(OldBufferingState, FCB_STATE_READCACHING_ENABLED) &&
664 !BooleanFlagOn(NewBufferingState, FCB_STATE_READCACHING_ENABLED)) ||
665 BooleanFlagOn(NewBufferingState, FCB_STATE_DELETE_ON_CLOSE))
666 {
667 DPRINT("Purging\n");
668
669 if (!NT_SUCCESS(Status))
670 {
671 DPRINT("Previous flush failed with status: %lx\n", Status);
672 }
673
674 CcPurgeCacheSection(&Fcb->NonPaged->SectionObjectPointers, NULL, 0, TRUE);
675 }
676
677 /* If there's already a change pending in SRV_OPEN */
678 if (ComputeNewState && BooleanFlagOn(SrvOpen->Flags, SRVOPEN_FLAG_BUFFERING_STATE_CHANGE_PENDING))
679 {
680 /* If there's a FOBX at least */
681 if (!IsListEmpty(&SrvOpen->FobxList))
682 {
683 PRX_CONTEXT RxContext;
684
685 /* Create a fake context to pass to the mini-rdr */
686 RxContext = RxCreateRxContext(NULL, Fcb->RxDeviceObject, RX_CONTEXT_FLAG_MUST_SUCCEED_NONBLOCKING | RX_CONTEXT_FLAG_WAIT);
687 if (RxContext != NULL)
688 {
689 PFOBX Fobx;
690
691 RxContext->pFcb = RX_GET_MRX_FCB(Fcb);
692
693 /* Give the first FOBX */
694 Fobx = CONTAINING_RECORD(SrvOpen->FobxList.Flink, FOBX, FobxQLinks);
695 RxContext->pFobx = (PMRX_FOBX)Fobx;
696 RxContext->pRelevantSrvOpen = Fobx->pSrvOpen;
697
698 /* If there was a delayed close, perform it */
699 if (BooleanFlagOn(SrvOpen->Flags, SRVOPEN_FLAG_CLOSE_DELAYED))
700 {
701 DPRINT("Oplock break close for %p\n", SrvOpen);
702
703 RxCloseAssociatedSrvOpen(Fobx, RxContext);
704 }
705 /* Otherwise, inform the mini-rdr about completion */
706 else
707 {
708 MINIRDR_CALL_THROUGH(MiniStatus, Fcb->MRxDispatch, MRxCompleteBufferingStateChangeRequest,
709 (RxContext, (PMRX_SRV_OPEN)SrvOpen, Context));
710 (void)MiniStatus;
711 }
712
713 RxDereferenceAndDeleteRxContext(RxContext);
714 }
715 }
716 }
717
718 /* Set the new state */
719 Fcb->FcbState ^= (NewBufferingState ^ Fcb->FcbState) & FCB_STATE_BUFFERING_STATE_MASK;
720 }
721 _SEH2_FINALLY
722 {
723 /* Job done, clear the flag */
724 ClearFlag(Fcb->FcbState, FCB_STATE_BUFFERSTATE_CHANGING);
725
726 if (!BooleanFlagOn(NewBufferingState, FCB_STATE_FILETIMECACHEING_ENABLED))
727 {
728 ClearFlag(Fcb->FcbState, FCB_STATE_TIME_AND_SIZE_ALREADY_SET);
729 }
730 }
731 _SEH2_END;
732
733 return Status;
734 }
735
736 NTSTATUS
737 RxCheckVNetRootCredentials(
738 PRX_CONTEXT RxContext,
739 PV_NET_ROOT VNetRoot,
740 PLUID LogonId,
741 PUNICODE_STRING UserName,
742 PUNICODE_STRING UserDomain,
743 PUNICODE_STRING Password,
744 ULONG Flags)
745 {
746 PAGED_CODE();
747
748 /* If that's a UNC name, there's nothing to process */
749 if (BooleanFlagOn(RxContext->Flags, RX_CONTEXT_CREATE_FLAG_UNC_NAME) &&
750 (BooleanFlagOn(VNetRoot->Flags, VNETROOT_FLAG_CSCAGENT_INSTANCE) ||
751 Flags != 0))
752 {
753 return STATUS_MORE_PROCESSING_REQUIRED;
754 }
755
756 /* Compare the logon ID in the VNetRoot with the one provided */
757 if (RtlCompareMemory(&VNetRoot->LogonId, LogonId, sizeof(LUID)) != sizeof(LUID))
758 {
759 return STATUS_MORE_PROCESSING_REQUIRED;
760 }
761
762 /* No credential provided? That's OK */
763 if (UserName == NULL && UserDomain == NULL && Password == NULL)
764 {
765 return STATUS_SUCCESS;
766 }
767
768 /* Left to do! */
769 UNIMPLEMENTED;
770 return STATUS_NOT_IMPLEMENTED;
771 }
772
773 NTSTATUS
774 RxCompleteRequest(
775 PRX_CONTEXT Context,
776 NTSTATUS Status)
777 {
778 PIRP Irp;
779
780 PAGED_CODE();
781
782 DPRINT("RxCompleteRequest(%p, %lx)\n", Context, Status);
783
784 ASSERT(Context != NULL);
785 ASSERT(Context->CurrentIrp != NULL);
786 Irp = Context->CurrentIrp;
787
788 /* Debug what the caller asks for */
789 if (Context->LoudCompletionString != NULL)
790 {
791 DPRINT("LoudCompletion: %lx/%lx with %wZ\n", Status, Irp->IoStatus.Information, Context->LoudCompletionString);
792 /* Does the user asks to stop on failed completion */
793 if (!NT_SUCCESS(Status) && RxStopOnLoudCompletion)
794 {
795 DPRINT1("LoudFailure: %lx/%lx with %wZ\n", Status, Irp->IoStatus.Information, Context->LoudCompletionString);
796 }
797 }
798
799 /* Complete for real */
800 Context->CurrentIrp = NULL;
801 RxCompleteRequest_Real(Context, Irp, Status);
802
803 DPRINT("Status: %lx\n", Status);
804 return Status;
805 }
806
807 /*
808 * @implemented
809 */
810 VOID
811 RxCompleteRequest_Real(
812 IN PRX_CONTEXT RxContext,
813 IN PIRP Irp,
814 IN NTSTATUS Status)
815 {
816 CCHAR Boost;
817 KIRQL OldIrql;
818 PIO_STACK_LOCATION Stack;
819
820 DPRINT("RxCompleteRequest_Real(%p, %p, %lx)\n", RxContext, Irp, Status);
821
822 /* Nothing to complete, just free context */
823 if (Irp == NULL)
824 {
825 DPRINT("NULL IRP for %p\n", RxContext);
826 if (RxContext != NULL)
827 {
828 RxDereferenceAndDeleteRxContext_Real(RxContext);
829 }
830
831 return;
832 }
833
834 /* Remove cancel routine */
835 IoAcquireCancelSpinLock(&OldIrql);
836 IoSetCancelRoutine(Irp, NULL);
837 IoReleaseCancelSpinLock(OldIrql);
838
839 /* Select the boost, given the success/paging operation */
840 if (NT_SUCCESS(Status) || !BooleanFlagOn(Irp->Flags, IRP_SYNCHRONOUS_PAGING_IO))
841 {
842 Boost = IO_DISK_INCREMENT;
843 }
844 else
845 {
846 Irp->IoStatus.Information = 0;
847 Boost = IO_NO_INCREMENT;
848 }
849 Irp->IoStatus.Status = Status;
850
851 if (RxContext != NULL)
852 {
853 ASSERT(RxContext->MajorFunction <= IRP_MJ_MAXIMUM_FUNCTION);
854 if (RxContext->MajorFunction != IRP_MJ_DEVICE_CONTROL)
855 {
856 DPRINT("Completing: MN: %d, Context: %p, IRP: %p, Status: %lx, Info: %lx, #%lx\n",
857 RxContext->MinorFunction, RxContext, Irp,
858 Status, Irp->IoStatus.Information, RxContext->SerialNumber);
859 }
860 }
861
862 /* If that's an opening, there might be a canonical name allocated,
863 * if completion isn't pending, release it
864 */
865 Stack = IoGetCurrentIrpStackLocation(Irp);
866 if (Stack->MajorFunction == IRP_MJ_CREATE && Status != STATUS_PENDING &&
867 RxContext != NULL)
868 {
869 if (BooleanFlagOn(RxContext->Create.Flags, 2))
870 {
871 Stack->FileObject->FileName.Length += sizeof(WCHAR);
872 }
873
874 RxpPrepareCreateContextForReuse(RxContext);
875 ASSERT(RxContext->Create.CanonicalNameBuffer == NULL);
876 }
877
878 /* If it's a write, validate the correct behavior of the operation */
879 if (Stack->MajorFunction == IRP_MJ_WRITE)
880 {
881 if (NT_SUCCESS(Irp->IoStatus.Status))
882 {
883 ASSERT(Irp->IoStatus.Information <= Stack->Parameters.Write.Length);
884 }
885 }
886
887 /* If it's pending, make sure IRP is marked as such */
888 if (RxContext != NULL)
889 {
890 if (RxContext->PendingReturned)
891 {
892 ASSERT(BooleanFlagOn(Stack->Control, SL_PENDING_RETURNED));
893 }
894 }
895
896 /* Complete now */
897 DPRINT("Completing IRP with %x/%x\n", Irp->IoStatus.Status, Irp->IoStatus.Information);
898 IoCompleteRequest(Irp, Boost);
899
900 /* If there's a context, dereference it */
901 if (RxContext != NULL)
902 {
903 RxDereferenceAndDeleteRxContext_Real(RxContext);
904 }
905 }
906
907 /*
908 * @implemented
909 */
910 VOID
911 RxCompleteSrvOpenKeyAssociation(
912 IN OUT PSRV_OPEN SrvOpen)
913 {
914 PSRV_CALL SrvCall;
915
916 SrvCall = (PSRV_CALL)((PFCB)SrvOpen->pFcb)->VNetRoot->pNetRoot->pSrvCall;
917 /* Only handle requests if opening was a success */
918 if (SrvOpen->Condition == Condition_Good)
919 {
920 KIRQL OldIrql;
921 BOOLEAN ProcessChange;
922 LIST_ENTRY DiscardedRequests;
923
924 /* Initialize our discarded requests list */
925 InitializeListHead(&DiscardedRequests);
926
927 RxAcquireBufferingManagerMutex(&SrvCall->BufferingManager);
928
929 /* Transfer our requests in the SRV_CALL */
930 RxTransferList(&SrvCall->BufferingManager.SrvOpenLists[0], &SrvOpen->SrvOpenKeyList);
931
932 /* Was increased in RxInitiateSrvOpenKeyAssociation(), opening is done */
933 InterlockedDecrement(&SrvCall->BufferingManager.NumberOfOutstandingOpens);
934
935 /* Dispatch requests and get the discarded ones */
936 RxpDispatchChangeBufferingStateRequests(SrvCall, SrvOpen, &DiscardedRequests);
937
938 RxReleaseBufferingManagerMutex(&SrvCall->BufferingManager);
939
940 /* Is there still anything to process? */
941 KeAcquireSpinLock(&SrvCall->BufferingManager.SpinLock, &OldIrql);
942 if (IsListEmpty(&SrvCall->BufferingManager.HandlerList))
943 {
944 ProcessChange = FALSE;
945 }
946 else
947 {
948 ProcessChange = (SrvCall->BufferingManager.HandlerInactive == FALSE);
949 if (ProcessChange)
950 {
951 SrvCall->BufferingManager.HandlerInactive = TRUE;
952 }
953 }
954 KeReleaseSpinLock(&SrvCall->BufferingManager.SpinLock, OldIrql);
955
956 /* Yes? Go ahead! */
957 if (ProcessChange)
958 {
959 RxReferenceSrvCall(SrvCall);
960 RxPostToWorkerThread(RxFileSystemDeviceObject, HyperCriticalWorkQueue,
961 &SrvCall->BufferingManager.HandlerWorkItem,
962 RxProcessChangeBufferingStateRequests, SrvCall);
963 }
964
965 /* And discard left requests */
966 RxpDiscardChangeBufferingStateRequests(&DiscardedRequests);
967 }
968 else
969 {
970 InterlockedDecrement(&SrvCall->BufferingManager.NumberOfOutstandingOpens);
971 }
972 }
973
974 /*
975 * @implemented
976 */
977 NTSTATUS
978 RxConstructNetRoot(
979 IN PRX_CONTEXT RxContext,
980 IN PSRV_CALL SrvCall,
981 IN PNET_ROOT NetRoot,
982 IN PV_NET_ROOT VirtualNetRoot,
983 OUT PLOCK_HOLDING_STATE LockHoldingState)
984 {
985 NTSTATUS Status;
986 PRX_PREFIX_TABLE PrefixTable;
987 PMRX_CREATENETROOT_CONTEXT Context;
988 RX_BLOCK_CONDITION RootCondition, VRootCondition;
989
990 PAGED_CODE();
991
992 DPRINT("RxConstructNetRoot(%p, %p, %p, %p, %p)\n", RxContext, SrvCall, NetRoot,
993 VirtualNetRoot, LockHoldingState);
994
995 /* Validate the lock is exclusively held */
996 PrefixTable = RxContext->RxDeviceObject->pRxNetNameTable;
997 ASSERT(*LockHoldingState == LHS_ExclusiveLockHeld);
998
999 /* Allocate the context */
1000 Context = RxAllocatePoolWithTag(PagedPool, sizeof(MRX_CREATENETROOT_CONTEXT), RX_SRVCALL_POOLTAG);
1001 if (Context == NULL)
1002 {
1003 return STATUS_INSUFFICIENT_RESOURCES;
1004 }
1005
1006 /* We can release lock now */
1007 RxReleasePrefixTableLock(PrefixTable);
1008 *LockHoldingState = LHS_LockNotHeld;
1009
1010 RootCondition = Condition_Bad;
1011 VRootCondition = Condition_Bad;
1012
1013 /* Initialize the context */
1014 RtlZeroMemory(Context, sizeof(MRX_CREATENETROOT_CONTEXT));
1015 KeInitializeEvent(&Context->FinishEvent, SynchronizationEvent, FALSE);
1016 Context->RxContext = RxContext;
1017 Context->pVNetRoot = VirtualNetRoot;
1018 Context->Callback = RxCreateNetRootCallBack;
1019
1020 /* And call the mini-rdr */
1021 MINIRDR_CALL_THROUGH(Status, SrvCall->RxDeviceObject->Dispatch, MRxCreateVNetRoot, (Context));
1022 if (Status == STATUS_PENDING)
1023 {
1024 /* Wait for the mini-rdr to be done */
1025 KeWaitForSingleObject(&Context->FinishEvent, Executive, KernelMode, FALSE, NULL);
1026 /* Update the structures condition according to mini-rdr return */
1027 if (NT_SUCCESS(Context->NetRootStatus))
1028 {
1029 if (NT_SUCCESS(Context->VirtualNetRootStatus))
1030 {
1031 RootCondition = Condition_Good;
1032 VRootCondition = Condition_Good;
1033 Status = STATUS_SUCCESS;
1034 }
1035 else
1036 {
1037 RootCondition = Condition_Good;
1038 Status = Context->VirtualNetRootStatus;
1039 }
1040 }
1041 else
1042 {
1043 Status = Context->VirtualNetRootStatus;
1044 if (NT_SUCCESS(Status))
1045 {
1046 Status = Context->NetRootStatus;
1047 }
1048 }
1049 }
1050 else
1051 {
1052 /* It has to return STATUS_PENDING! */
1053 ASSERT(FALSE);
1054 }
1055
1056 /* Acquire lock again - for caller lock status will remain unchanged */
1057 ASSERT(*LockHoldingState == LHS_LockNotHeld);
1058 RxAcquirePrefixTableLockExclusive(PrefixTable, TRUE);
1059 *LockHoldingState = LHS_ExclusiveLockHeld;
1060
1061 /* Do the transition to the condition got from mini-rdr */
1062 RxTransitionNetRoot(NetRoot, RootCondition);
1063 RxTransitionVNetRoot(VirtualNetRoot, VRootCondition);
1064
1065 /* Context is not longer needed */
1066 RxFreePoolWithTag(Context, RX_SRVCALL_POOLTAG);
1067
1068 DPRINT("Status: %x\n", Status);
1069
1070 return Status;
1071 }
1072
1073 /*
1074 * @implemented
1075 */
1076 NTSTATUS
1077 RxConstructSrvCall(
1078 IN PRX_CONTEXT RxContext,
1079 IN PSRV_CALL SrvCall,
1080 OUT PLOCK_HOLDING_STATE LockHoldingState)
1081 {
1082 NTSTATUS Status;
1083 PRX_PREFIX_TABLE PrefixTable;
1084 PRDBSS_DEVICE_OBJECT RxDeviceObject;
1085 PMRX_SRVCALLDOWN_STRUCTURE Calldown;
1086 PMRX_SRVCALL_CALLBACK_CONTEXT CallbackContext;
1087
1088 PAGED_CODE();
1089
1090 DPRINT("RxConstructSrvCall(%p, %p, %p)\n", RxContext, SrvCall, LockHoldingState);
1091
1092 /* Validate the lock is exclusively held */
1093 RxDeviceObject = RxContext->RxDeviceObject;
1094 PrefixTable = RxDeviceObject->pRxNetNameTable;
1095 ASSERT(*LockHoldingState == LHS_ExclusiveLockHeld);
1096
1097 /* Allocate the context for mini-rdr */
1098 Calldown = RxAllocatePoolWithTag(NonPagedPool, sizeof(MRX_SRVCALLDOWN_STRUCTURE), RX_SRVCALL_POOLTAG);
1099 if (Calldown == NULL)
1100 {
1101 SrvCall->Context = NULL;
1102 SrvCall->Condition = Condition_Bad;
1103 RxReleasePrefixTableLock(PrefixTable);
1104 *LockHoldingState = LHS_LockNotHeld;
1105 return STATUS_INSUFFICIENT_RESOURCES;
1106 }
1107
1108 /* Initialize it */
1109 RtlZeroMemory(Calldown, sizeof(MRX_SRVCALLDOWN_STRUCTURE));
1110
1111 SrvCall->Context = NULL;
1112 SrvCall->Condition = Condition_InTransition;
1113
1114 RxReleasePrefixTableLock(PrefixTable);
1115 *LockHoldingState = LHS_LockNotHeld;
1116
1117 CallbackContext = &Calldown->CallbackContexts[0];
1118 DPRINT("CalldownContext %p for %wZ\n", CallbackContext, &RxDeviceObject->DeviceName);
1119 DPRINT("With calldown %p and SrvCall %p\n", Calldown, SrvCall);
1120 CallbackContext->SrvCalldownStructure = Calldown;
1121 CallbackContext->CallbackContextOrdinal = 0;
1122 CallbackContext->RxDeviceObject = RxDeviceObject;
1123
1124 RxReferenceSrvCall(SrvCall);
1125
1126 /* If we're async, we'll post, otherwise, we'll have to wait for completion */
1127 if (BooleanFlagOn(RxContext->Flags, RX_CONTEXT_FLAG_ASYNC_OPERATION))
1128 {
1129 RxPrePostIrp(RxContext, RxContext->CurrentIrp);
1130 }
1131 else
1132 {
1133 KeInitializeEvent(&Calldown->FinishEvent, SynchronizationEvent, FALSE);
1134 }
1135
1136 Calldown->NumberToWait = 1;
1137 Calldown->NumberRemaining = 1;
1138 Calldown->RxContext = RxContext;
1139 Calldown->SrvCall = (PMRX_SRV_CALL)SrvCall;
1140 Calldown->CallBack = RxCreateSrvCallCallBack;
1141 Calldown->BestFinisher = NULL;
1142 CallbackContext->Status = STATUS_BAD_NETWORK_PATH;
1143 InitializeListHead(&Calldown->SrvCalldownList);
1144
1145 /* Call the mini-rdr */
1146 ASSERT(RxDeviceObject->Dispatch != NULL);
1147 ASSERT(NodeType(RxDeviceObject->Dispatch) == RDBSS_NTC_MINIRDR_DISPATCH);
1148 ASSERT(RxDeviceObject->Dispatch->MRxCreateSrvCall != NULL);
1149 Status = RxDeviceObject->Dispatch->MRxCreateSrvCall((PMRX_SRV_CALL)SrvCall, CallbackContext);
1150 /* It has to return STATUS_PENDING! */
1151 ASSERT(Status == STATUS_PENDING);
1152
1153 /* No async, start completion */
1154 if (!BooleanFlagOn(RxContext->Flags, RX_CONTEXT_FLAG_ASYNC_OPERATION))
1155 {
1156 KeWaitForSingleObject(&Calldown->FinishEvent, Executive, KernelMode, FALSE, NULL);
1157
1158 /* Finish construction - we'll notify mini-rdr it's the winner */
1159 Status = RxFinishSrvCallConstruction(Calldown);
1160 if (!NT_SUCCESS(Status))
1161 {
1162 RxReleasePrefixTableLock(PrefixTable);
1163 *LockHoldingState = LHS_LockNotHeld;
1164 }
1165 else
1166 {
1167 ASSERT(RxIsPrefixTableLockAcquired(PrefixTable));
1168 *LockHoldingState = LHS_ExclusiveLockHeld;
1169 }
1170 }
1171
1172 DPRINT("RxConstructSrvCall() = Status: %x\n", Status);
1173 return Status;
1174 }
1175
1176 /*
1177 * @implemented
1178 */
1179 NTSTATUS
1180 RxConstructVirtualNetRoot(
1181 IN PRX_CONTEXT RxContext,
1182 IN PUNICODE_STRING CanonicalName,
1183 IN NET_ROOT_TYPE NetRootType,
1184 OUT PV_NET_ROOT *VirtualNetRootPointer,
1185 OUT PLOCK_HOLDING_STATE LockHoldingState,
1186 OUT PRX_CONNECTION_ID RxConnectionId)
1187 {
1188 NTSTATUS Status;
1189 PV_NET_ROOT VNetRoot;
1190 RX_BLOCK_CONDITION Condition;
1191 UNICODE_STRING LocalNetRootName, FilePathName;
1192
1193 PAGED_CODE();
1194
1195 ASSERT(*LockHoldingState != LHS_LockNotHeld);
1196
1197 VNetRoot = NULL;
1198 Condition = Condition_Bad;
1199 /* Before creating the VNetRoot, try to find the appropriate connection */
1200 Status = RxFindOrCreateConnections(RxContext, CanonicalName, NetRootType,
1201 &LocalNetRootName, &FilePathName,
1202 LockHoldingState, RxConnectionId);
1203 /* Found and active */
1204 if (Status == STATUS_CONNECTION_ACTIVE)
1205 {
1206 /* We need a new VNetRoot */
1207 VNetRoot = RxCreateVNetRoot(RxContext, (PNET_ROOT)RxContext->Create.pVNetRoot->pNetRoot,
1208 CanonicalName, &LocalNetRootName, &FilePathName, RxConnectionId);
1209 if (VNetRoot != NULL)
1210 {
1211 RxReferenceVNetRoot(VNetRoot);
1212 }
1213
1214 /* Dereference previous VNetRoot */
1215 RxDereferenceVNetRoot(RxContext->Create.pVNetRoot->pNetRoot, *LockHoldingState);
1216 /* Reset and start construct (new structures will replace old ones) */
1217 RxContext->Create.pSrvCall = NULL;
1218 RxContext->Create.pNetRoot = NULL;
1219 RxContext->Create.pVNetRoot = NULL;
1220
1221 /* Construct new NetRoot */
1222 if (VNetRoot != NULL)
1223 {
1224 Status = RxConstructNetRoot(RxContext, (PSRV_CALL)VNetRoot->pNetRoot->pSrvCall,
1225 (PNET_ROOT)VNetRoot->pNetRoot, VNetRoot, LockHoldingState);
1226 if (NT_SUCCESS(Status))
1227 {
1228 Condition = Condition_Good;
1229 }
1230 }
1231 else
1232 {
1233 Status = STATUS_INSUFFICIENT_RESOURCES;
1234 }
1235 }
1236 else
1237 {
1238 /* If it failed creating the connection, leave */
1239 if (Status != STATUS_SUCCESS)
1240 {
1241 if (*LockHoldingState != LHS_LockNotHeld)
1242 {
1243 RxReleasePrefixTableLock(RxContext->RxDeviceObject->pRxNetNameTable);
1244 *LockHoldingState = LHS_LockNotHeld;
1245 }
1246
1247 *VirtualNetRootPointer = VNetRoot;
1248 DPRINT("RxConstructVirtualNetRoot() = Status: %x\n", Status);
1249 return Status;
1250 }
1251
1252 *LockHoldingState = LHS_ExclusiveLockHeld;
1253
1254 VNetRoot = (PV_NET_ROOT)RxContext->Create.pVNetRoot;
1255 Condition = Condition_Good;
1256 }
1257
1258 /* We have a non stable VNetRoot - transition it */
1259 if (VNetRoot != NULL && !StableCondition(VNetRoot->Condition))
1260 {
1261 RxTransitionVNetRoot(VNetRoot, Condition);
1262 }
1263
1264 /* If recreation failed */
1265 if (Status != STATUS_SUCCESS)
1266 {
1267 /* Dereference potential VNetRoot */
1268 if (VNetRoot != NULL)
1269 {
1270 ASSERT(*LockHoldingState != LHS_LockNotHeld);
1271 RxDereferenceVNetRoot(VNetRoot, *LockHoldingState);
1272 VNetRoot = NULL;
1273 }
1274
1275 /* Release lock */
1276 if (*LockHoldingState != LHS_LockNotHeld)
1277 {
1278 RxReleasePrefixTableLock(RxContext->RxDeviceObject->pRxNetNameTable);
1279 *LockHoldingState = LHS_LockNotHeld;
1280 }
1281
1282 /* Set NULL ptr */
1283 *VirtualNetRootPointer = VNetRoot;
1284 return Status;
1285 }
1286
1287 /* Return the allocated VNetRoot */
1288 *VirtualNetRootPointer = VNetRoot;
1289 return Status;
1290 }
1291
1292 /*
1293 * @implemented
1294 */
1295 PFCB
1296 RxCreateNetFcb(
1297 IN PRX_CONTEXT RxContext,
1298 IN PV_NET_ROOT VNetRoot,
1299 IN PUNICODE_STRING Name)
1300 {
1301 PFCB Fcb;
1302 BOOLEAN FakeFcb;
1303 PNET_ROOT NetRoot;
1304 POOL_TYPE PoolType;
1305 NODE_TYPE_CODE NodeType;
1306 PIO_STACK_LOCATION Stack;
1307 PRDBSS_DEVICE_OBJECT RxDeviceObject;
1308
1309 PAGED_CODE();
1310
1311 /* We need a decent VNetRoot */
1312 ASSERT(VNetRoot != NULL && NodeType(VNetRoot) == RDBSS_NTC_V_NETROOT);
1313
1314 NetRoot = (PNET_ROOT)VNetRoot->pNetRoot;
1315 ASSERT(NodeType(NetRoot) == RDBSS_NTC_NETROOT);
1316 ASSERT((PMRX_NET_ROOT)NetRoot == RxContext->Create.pNetRoot);
1317
1318 RxDeviceObject = NetRoot->pSrvCall->RxDeviceObject;
1319 ASSERT(RxDeviceObject == RxContext->RxDeviceObject);
1320
1321 Stack = RxContext->CurrentIrpSp;
1322
1323 /* Do we need to create a fake FCB? Like for renaming */
1324 FakeFcb = BooleanFlagOn(Stack->Flags, SL_OPEN_TARGET_DIRECTORY) &&
1325 !BooleanFlagOn(NetRoot->Flags, NETROOT_FLAG_SUPPORTS_SYMBOLIC_LINKS);
1326 ASSERT(FakeFcb || RxIsFcbTableLockExclusive(&NetRoot->FcbTable));
1327
1328 PoolType = (BooleanFlagOn(Stack->Flags, SL_OPEN_PAGING_FILE) ? NonPagedPool : PagedPool);
1329 NodeType = (FakeFcb) ? RDBSS_NTC_OPENTARGETDIR_FCB : RDBSS_STORAGE_NTC(FileTypeNotYetKnown);
1330
1331 /* Allocate the FCB */
1332 Fcb = RxAllocateFcbObject(RxDeviceObject, NodeType, PoolType,
1333 NetRoot->InnerNamePrefix.Length + Name->Length, NULL);
1334 if (Fcb == NULL)
1335 {
1336 return NULL;
1337 }
1338
1339 /* Initialize the FCB */
1340 Fcb->CachedNetRootType = NetRoot->Type;
1341 Fcb->RxDeviceObject = RxDeviceObject;
1342 Fcb->MRxDispatch = RxDeviceObject->Dispatch;
1343 Fcb->VNetRoot = VNetRoot;
1344 Fcb->pNetRoot = VNetRoot->pNetRoot;
1345
1346 InitializeListHead(&Fcb->SrvOpenList);
1347 Fcb->SrvOpenListVersion = 0;
1348
1349 Fcb->FcbTableEntry.Path.Length = Name->Length;
1350 Fcb->FcbTableEntry.Path.MaximumLength = Name->Length;
1351 Fcb->FcbTableEntry.Path.Buffer = Add2Ptr(Fcb->PrivateAlreadyPrefixedName.Buffer, NetRoot->InnerNamePrefix.Length);
1352 RtlMoveMemory(Fcb->PrivateAlreadyPrefixedName.Buffer, NetRoot->InnerNamePrefix.Buffer,
1353 NetRoot->InnerNamePrefix.Length);
1354 RtlMoveMemory(Fcb->FcbTableEntry.Path.Buffer, Name->Buffer, Name->Length);
1355
1356 /* Copy back parameters from RxContext */
1357 if (BooleanFlagOn(RxContext->Create.Flags, RX_CONTEXT_CREATE_FLAG_ADDEDBACKSLASH))
1358 {
1359 Fcb->FcbState |= FCB_STATE_ADDEDBACKSLASH;
1360 }
1361
1362 InitializeListHead(&Fcb->NonPaged->TransitionWaitList);
1363
1364 if (BooleanFlagOn(Stack->Flags, SL_OPEN_PAGING_FILE))
1365 {
1366 Fcb->FcbState |= FCB_STATE_PAGING_FILE;
1367 }
1368
1369 if (RxContext->MajorFunction == IRP_MJ_CREATE && BooleanFlagOn(RxContext->Create.Flags, RX_CONTEXT_CREATE_FLAG_SPECIAL_PATH))
1370 {
1371 Fcb->FcbState |= FCB_STATE_SPECIAL_PATH;
1372 }
1373
1374 Fcb->Header.Resource = &Fcb->NonPaged->HeaderResource;
1375 ExInitializeResourceLite(Fcb->Header.Resource);
1376
1377 Fcb->Header.PagingIoResource = &Fcb->NonPaged->PagingIoResource;
1378 ExInitializeResourceLite(Fcb->Header.PagingIoResource);
1379
1380 Fcb->BufferedLocks.Resource = &Fcb->NonPaged->BufferedLocksResource;
1381 ExInitializeResourceLite(Fcb->BufferedLocks.Resource);
1382
1383 /* Fake FCB doesn't go in prefix table */
1384 if (FakeFcb)
1385 {
1386 Fcb->FcbState |= (FCB_STATE_FAKEFCB | FCB_STATE_NAME_ALREADY_REMOVED);
1387 InitializeListHead(&Fcb->FcbTableEntry.HashLinks);
1388 DPRINT("Fake FCB: %p\n", Fcb);
1389 }
1390 else
1391 {
1392 RxFcbTableInsertFcb(&NetRoot->FcbTable, Fcb);
1393 }
1394
1395 RxReferenceVNetRoot(VNetRoot);
1396 InterlockedIncrement((volatile long *)&Fcb->pNetRoot->NumberOfFcbs);
1397
1398 Fcb->ulFileSizeVersion = 0;
1399
1400 DPRINT("FCB %p for %wZ\n", Fcb, &Fcb->FcbTableEntry.Path);
1401 RxReferenceNetFcb(Fcb);
1402
1403 return Fcb;
1404 }
1405
1406 /*
1407 * @implemented
1408 */
1409 PMRX_FOBX
1410 NTAPI
1411 RxCreateNetFobx(
1412 OUT PRX_CONTEXT RxContext,
1413 IN PMRX_SRV_OPEN MrxSrvOpen)
1414 {
1415 PFCB Fcb;
1416 PFOBX Fobx;
1417 ULONG Flags;
1418 PNET_ROOT NetRoot;
1419 PSRV_OPEN SrvOpen;
1420 POOL_TYPE PoolType;
1421
1422 PAGED_CODE();
1423
1424 SrvOpen = (PSRV_OPEN)MrxSrvOpen;
1425 ASSERT(NodeType(SrvOpen) == RDBSS_NTC_SRVOPEN);
1426 ASSERT(NodeTypeIsFcb(SrvOpen->Fcb));
1427 ASSERT(RxIsFcbAcquiredExclusive(SrvOpen->Fcb));
1428
1429 Fcb = SrvOpen->Fcb;
1430 PoolType = (BooleanFlagOn(Fcb->FcbState, FCB_STATE_PAGING_FILE) ? NonPagedPool : PagedPool);
1431 /* Can we use pre-allocated FOBX? */
1432 if (!BooleanFlagOn(Fcb->FcbState, FCB_STATE_FOBX_USED) && Fcb->InternalSrvOpen == (PSRV_OPEN)MrxSrvOpen)
1433 {
1434 Fobx = Fcb->InternalFobx;
1435 /* Call allocate to initialize the FOBX */
1436 RxAllocateFcbObject(Fcb->RxDeviceObject, RDBSS_NTC_FOBX, PoolType, 0, Fobx);
1437 /* Mark it used now */
1438 Fcb->FcbState |= FCB_STATE_FOBX_USED;
1439 Flags = FOBX_FLAG_ENCLOSED_ALLOCATED;
1440 }
1441 else if (!BooleanFlagOn(SrvOpen->Flags, SRVOPEN_FLAG_FOBX_USED))
1442 {
1443 Fobx = SrvOpen->InternalFobx;
1444 /* Call allocate to initialize the FOBX */
1445 RxAllocateFcbObject(Fcb->RxDeviceObject, RDBSS_NTC_FOBX, PoolType, 0, Fobx);
1446 /* Mark it used now */
1447 SrvOpen->Flags |= SRVOPEN_FLAG_FOBX_USED;
1448 Flags = FOBX_FLAG_ENCLOSED_ALLOCATED;
1449 }
1450 else
1451 {
1452 /* Last case, we cannot, allocate a FOBX */
1453 Fobx = RxAllocateFcbObject(Fcb->RxDeviceObject, RDBSS_NTC_FOBX, PoolType, 0, NULL);
1454 Flags = 0;
1455 }
1456
1457 /* Allocation failed! */
1458 if (Fobx == NULL)
1459 {
1460 return NULL;
1461 }
1462
1463 /* Set flags */
1464 Fobx->Flags = Flags;
1465
1466 /* Initialize throttling */
1467 NetRoot = (PNET_ROOT)RxContext->Create.pNetRoot;
1468 if (NetRoot != NULL)
1469 {
1470 if (NetRoot->DeviceType == FILE_DEVICE_DISK)
1471 {
1472 RxInitializeThrottlingState(&Fobx->Specific.DiskFile.LockThrottlingState,
1473 NetRoot->DiskParameters.LockThrottlingParameters.Increment,
1474 NetRoot->DiskParameters.LockThrottlingParameters.MaximumDelay);
1475 }
1476 else if (NetRoot->DeviceType == FILE_DEVICE_NAMED_PIPE)
1477 {
1478 RxInitializeThrottlingState(&Fobx->Specific.NamedPipe.ThrottlingState,
1479 NetRoot->NamedPipeParameters.PipeReadThrottlingParameters.Increment,
1480 NetRoot->NamedPipeParameters.PipeReadThrottlingParameters.MaximumDelay);
1481 }
1482 }
1483
1484 /* Propagate flags fron RxContext */
1485 if (BooleanFlagOn(RxContext->Create.Flags, RX_CONTEXT_CREATE_FLAG_UNC_NAME))
1486 {
1487 Fobx->Flags |= FOBX_FLAG_UNC_NAME;
1488 }
1489
1490 if (BooleanFlagOn(RxContext->Create.NtCreateParameters.CreateOptions, FILE_OPEN_FOR_BACKUP_INTENT))
1491 {
1492 Fobx->Flags |= FOBX_FLAG_BACKUP_INTENT;
1493 }
1494
1495 /* Continue init */
1496 Fobx->FobxSerialNumber = 0;
1497 Fobx->SrvOpen = (PSRV_OPEN)MrxSrvOpen;
1498 Fobx->NodeReferenceCount = 1;
1499 Fobx->RxDeviceObject = Fcb->RxDeviceObject;
1500
1501 RxReferenceSrvOpen(SrvOpen);
1502 InterlockedIncrement((volatile long *)&SrvOpen->pVNetRoot->NumberOfFobxs);
1503
1504 InsertTailList(&SrvOpen->FobxList, &Fobx->FobxQLinks);
1505 InitializeListHead(&Fobx->ScavengerFinalizationList);
1506 InitializeListHead(&Fobx->ClosePendingList);
1507
1508 Fobx->CloseTime.QuadPart = 0;
1509 Fobx->fOpenCountDecremented = FALSE;
1510
1511 DPRINT("FOBX %p for SRV_OPEN %p FCB %p\n", Fobx, Fobx->SrvOpen, Fobx->SrvOpen->pFcb);
1512
1513 return (PMRX_FOBX)Fobx;
1514 }
1515
1516 /*
1517 * @implemented
1518 */
1519 PNET_ROOT
1520 RxCreateNetRoot(
1521 IN PSRV_CALL SrvCall,
1522 IN PUNICODE_STRING Name,
1523 IN ULONG NetRootFlags,
1524 IN PRX_CONNECTION_ID OPTIONAL RxConnectionId)
1525 {
1526 PNET_ROOT NetRoot;
1527 USHORT CaseInsensitiveLength;
1528 PRX_PREFIX_TABLE PrefixTable;
1529
1530 DPRINT("RxCreateNetRoot(%p, %wZ, %x, %p)\n", SrvCall, Name, NetRootFlags, RxConnectionId);
1531
1532 PAGED_CODE();
1533
1534 /* We need a SRV_CALL */
1535 ASSERT(SrvCall != NULL);
1536
1537 PrefixTable = SrvCall->RxDeviceObject->pRxNetNameTable;
1538 ASSERT(RxIsPrefixTableLockExclusive(PrefixTable));
1539
1540 /* Get name length */
1541 CaseInsensitiveLength = SrvCall->PrefixEntry.Prefix.Length + Name->Length;
1542 if (CaseInsensitiveLength > MAXUSHORT)
1543 {
1544 return NULL;
1545 }
1546
1547 /* Allocate the NetRoot */
1548 NetRoot = RxAllocateObject(RDBSS_NTC_NETROOT, SrvCall->RxDeviceObject->Dispatch,
1549 CaseInsensitiveLength);
1550 if (NetRoot == NULL)
1551 {
1552 return NULL;
1553 }
1554
1555 /* Construct name */
1556 RtlMoveMemory(Add2Ptr(NetRoot->PrefixEntry.Prefix.Buffer, SrvCall->PrefixEntry.Prefix.Length),
1557 Name->Buffer, Name->Length);
1558 if (SrvCall->PrefixEntry.Prefix.Length != 0)
1559 {
1560 RtlMoveMemory(NetRoot->PrefixEntry.Prefix.Buffer, SrvCall->PrefixEntry.Prefix.Buffer,
1561 SrvCall->PrefixEntry.Prefix.Length);
1562 }
1563
1564 if (!BooleanFlagOn(SrvCall->Flags, SRVCALL_FLAG_CASE_INSENSITIVE_NETROOTS))
1565 {
1566 CaseInsensitiveLength = SrvCall->PrefixEntry.CaseInsensitiveLength;
1567 }
1568 /* Inisert in prefix table */
1569 RxPrefixTableInsertName(PrefixTable, &NetRoot->PrefixEntry, NetRoot,
1570 (PULONG)&NetRoot->NodeReferenceCount, CaseInsensitiveLength,
1571 RxConnectionId);
1572
1573 /* Prepare the FCB table */
1574 RxInitializeFcbTable(&NetRoot->FcbTable, TRUE);
1575
1576 InitializeListHead(&NetRoot->TransitionWaitList);
1577 InitializeListHead(&NetRoot->ScavengerFinalizationList);
1578 InitializeListHead(&NetRoot->VirtualNetRoots);
1579
1580 RxInitializePurgeSyncronizationContext(&NetRoot->PurgeSyncronizationContext);
1581
1582 NetRoot->SerialNumberForEnum = SerialNumber++;
1583 NetRoot->Flags |= NetRootFlags;
1584 NetRoot->DiskParameters.ClusterSize = 1;
1585 NetRoot->DiskParameters.ReadAheadGranularity = ReadAheadGranularity;
1586 NetRoot->SrvCall = SrvCall;
1587
1588 RxReferenceSrvCall(SrvCall);
1589
1590 DPRINT("NetRootName: %wZ (%p)\n", NetRoot->pNetRootName, NetRoot);
1591 return NetRoot;
1592 }
1593
1594 /*
1595 * @implemented
1596 */
1597 VOID
1598 NTAPI
1599 RxCreateNetRootCallBack(
1600 IN PMRX_CREATENETROOT_CONTEXT CreateNetRootContext)
1601 {
1602 PAGED_CODE();
1603
1604 KeSetEvent(&CreateNetRootContext->FinishEvent, IO_NETWORK_INCREMENT, FALSE);
1605 }
1606
1607 /*
1608 * @implemented
1609 */
1610 PRX_CONTEXT
1611 NTAPI
1612 RxCreateRxContext(
1613 IN PIRP Irp,
1614 IN PRDBSS_DEVICE_OBJECT RxDeviceObject,
1615 IN ULONG InitialContextFlags)
1616 {
1617 KIRQL OldIrql;
1618 PRX_CONTEXT Context;
1619
1620 ASSERT(RxDeviceObject != NULL);
1621
1622 DPRINT("RxCreateRxContext(%p, %p, %u)\n", Irp, RxDeviceObject, InitialContextFlags);
1623
1624 InterlockedIncrement((volatile LONG *)&RxFsdEntryCount);
1625 InterlockedIncrement((volatile LONG *)&RxDeviceObject->NumberOfActiveContexts);
1626
1627 /* Allocate the context from our lookaside list */
1628 Context = ExAllocateFromNPagedLookasideList(&RxContextLookasideList);
1629 if (Context == NULL)
1630 {
1631 return NULL;
1632 }
1633
1634 /* And initialize it */
1635 RtlZeroMemory(Context, sizeof(RX_CONTEXT));
1636 RxInitializeContext(Irp, RxDeviceObject, InitialContextFlags, Context);
1637 ASSERT((Context->MajorFunction != IRP_MJ_CREATE) || !BooleanFlagOn(Context->Flags, RX_CONTEXT_FLAG_MUST_SUCCEED_ALLOCATED));
1638
1639 /* Add it to our global list */
1640 KeAcquireSpinLock(&RxStrucSupSpinLock, &OldIrql);
1641 InsertTailList(&RxActiveContexts, &Context->ContextListEntry);
1642 KeReleaseSpinLock(&RxStrucSupSpinLock, OldIrql);
1643
1644 DPRINT("Context: %p\n", Context);
1645 return Context;
1646 }
1647
1648 /*
1649 * @implemented
1650 */
1651 PSRV_CALL
1652 RxCreateSrvCall(
1653 IN PRX_CONTEXT RxContext,
1654 IN PUNICODE_STRING Name,
1655 IN PUNICODE_STRING InnerNamePrefix OPTIONAL,
1656 IN PRX_CONNECTION_ID RxConnectionId)
1657 {
1658 ULONG NameLength;
1659 PSRV_CALL SrvCall;
1660
1661 PAGED_CODE();
1662
1663 DPRINT("RxCreateSrvCall(%p, %wZ, %wZ, %p)\n", RxContext, Name, InnerNamePrefix, RxConnectionId);
1664
1665 ASSERT(RxIsPrefixTableLockExclusive(RxContext->RxDeviceObject->pRxNetNameTable));
1666
1667 /* Get the name length */
1668 NameLength = Name->Length + 2 * sizeof(WCHAR);
1669 if (InnerNamePrefix != NULL)
1670 {
1671 NameLength += InnerNamePrefix->Length;
1672 }
1673
1674 /* Allocate the object */
1675 SrvCall = RxAllocateObject(RDBSS_NTC_SRVCALL, NULL, NameLength);
1676 if (SrvCall == NULL)
1677 {
1678 return NULL;
1679 }
1680
1681 /* Initialize it */
1682 SrvCall->SerialNumberForEnum = SerialNumber++;
1683 SrvCall->RxDeviceObject = RxContext->RxDeviceObject;
1684 RxInitializeBufferingManager(SrvCall);
1685 InitializeListHead(&SrvCall->TransitionWaitList);
1686 InitializeListHead(&SrvCall->ScavengerFinalizationList);
1687 RxInitializePurgeSyncronizationContext(&SrvCall->PurgeSyncronizationContext);
1688 RxInitializeSrvCallParameters(RxContext, SrvCall);
1689 RtlMoveMemory(SrvCall->PrefixEntry.Prefix.Buffer, Name->Buffer, Name->Length);
1690 SrvCall->PrefixEntry.Prefix.MaximumLength = Name->Length + 2 * sizeof(WCHAR);
1691 SrvCall->PrefixEntry.Prefix.Length = Name->Length;
1692 RxPrefixTableInsertName(RxContext->RxDeviceObject->pRxNetNameTable, &SrvCall->PrefixEntry,
1693 SrvCall, (PULONG)&SrvCall->NodeReferenceCount, Name->Length, RxConnectionId);
1694
1695 DPRINT("SrvCallName: %wZ (%p)\n", SrvCall->pSrvCallName, SrvCall);
1696 return SrvCall;
1697 }
1698
1699 /*
1700 * @implemented
1701 */
1702 VOID
1703 NTAPI
1704 RxCreateSrvCallCallBack(
1705 IN OUT PMRX_SRVCALL_CALLBACK_CONTEXT Context)
1706 {
1707 KIRQL OldIrql;
1708 PSRV_CALL SrvCall;
1709 PRX_CONTEXT RxContext;
1710 ULONG NumberRemaining;
1711 BOOLEAN StartDispatcher;
1712 PMRX_SRVCALLDOWN_STRUCTURE Calldown;
1713
1714 DPRINT("RxCreateSrvCallCallBack(%p)\n", Context);
1715
1716 /* Get our context structures */
1717 Calldown = Context->SrvCalldownStructure;
1718 SrvCall = (PSRV_CALL)Calldown->SrvCall;
1719
1720 /* If it is a success, that's the winner */
1721 KeAcquireSpinLock(&RxStrucSupSpinLock, &OldIrql);
1722 if (Context->Status == STATUS_SUCCESS)
1723 {
1724 Calldown->BestFinisherOrdinal = Context->CallbackContextOrdinal;
1725 Calldown->BestFinisher = Context->RxDeviceObject;
1726 }
1727 NumberRemaining = --Calldown->NumberRemaining;
1728 SrvCall->Status = Context->Status;
1729 KeReleaseSpinLock(&RxStrucSupSpinLock, OldIrql);
1730
1731 /* Still some to ask, keep going */
1732 if (NumberRemaining != 0)
1733 {
1734 return;
1735 }
1736
1737 /* If that's not async, signal we're done */
1738 RxContext = Calldown->RxContext;
1739 if (!BooleanFlagOn(RxContext->Flags, RX_CONTEXT_FLAG_ASYNC_OPERATION))
1740 {
1741 KeSetEvent(&Calldown->FinishEvent, IO_NETWORK_INCREMENT, FALSE);
1742 return;
1743 }
1744 /* If that's a mailslot, finish construction, no more to do */
1745 else if (BooleanFlagOn(RxContext->Flags, RX_CONTEXT_FLAG_CREATE_MAILSLOT))
1746 {
1747 RxFinishSrvCallConstruction(Calldown);
1748 return;
1749 }
1750
1751 /* Queue our finish call for delayed completion */
1752 DPRINT("Queuing RxFinishSrvCallConstruction() call\n");
1753 KeAcquireSpinLock(&RxStrucSupSpinLock, &OldIrql);
1754 InsertTailList(&RxSrvCalldownList, &Calldown->SrvCalldownList);
1755 StartDispatcher = !RxSrvCallConstructionDispatcherActive;
1756 KeReleaseSpinLock(&RxStrucSupSpinLock, OldIrql);
1757
1758 /* If we have to start dispatcher, go ahead */
1759 if (StartDispatcher)
1760 {
1761 NTSTATUS Status;
1762
1763 Status = RxDispatchToWorkerThread(RxFileSystemDeviceObject, CriticalWorkQueue,
1764 RxFinishSrvCallConstructionDispatcher, &RxSrvCalldownList);
1765 if (!NT_SUCCESS(Status))
1766 {
1767 /* It failed - run it manually.... */
1768 RxFinishSrvCallConstructionDispatcher(NULL);
1769 }
1770 }
1771 }
1772
1773 /*
1774 * @implemented
1775 */
1776 PSRV_OPEN
1777 RxCreateSrvOpen(
1778 IN PV_NET_ROOT VNetRoot,
1779 IN OUT PFCB Fcb)
1780 {
1781 ULONG Flags;
1782 PSRV_OPEN SrvOpen;
1783 POOL_TYPE PoolType;
1784
1785 PAGED_CODE();
1786
1787 ASSERT(NodeTypeIsFcb(Fcb));
1788 ASSERT(RxIsFcbAcquiredExclusive(Fcb));
1789
1790 PoolType = (BooleanFlagOn(Fcb->FcbState, FCB_STATE_PAGING_FILE) ? NonPagedPool : PagedPool);
1791
1792 _SEH2_TRY
1793 {
1794 SrvOpen = Fcb->InternalSrvOpen;
1795 /* Check whethet we have to allocate a new SRV_OPEN */
1796 if (Fcb->InternalSrvOpen == NULL || BooleanFlagOn(Fcb->FcbState, FCB_STATE_SRVOPEN_USED) ||
1797 BooleanFlagOn(Fcb->InternalSrvOpen->Flags, SRVOPEN_FLAG_ENCLOSED_ALLOCATED) ||
1798 !IsListEmpty(&Fcb->InternalSrvOpen->SrvOpenQLinks))
1799 {
1800 /* Proceed */
1801 SrvOpen = RxAllocateFcbObject(Fcb->VNetRoot->NetRoot->pSrvCall->RxDeviceObject,
1802 RDBSS_NTC_SRVOPEN, PoolType, 0, NULL);
1803 Flags = 0;
1804 }
1805 else
1806 {
1807 /* Otherwise, just use internal one and initialize it */
1808 RxAllocateFcbObject(Fcb->VNetRoot->NetRoot->pSrvCall->RxDeviceObject,
1809 RDBSS_NTC_INTERNAL_SRVOPEN, PoolType, 0,
1810 Fcb->InternalSrvOpen);
1811 Fcb->FcbState |= FCB_STATE_SRVOPEN_USED;
1812 Flags = SRVOPEN_FLAG_ENCLOSED_ALLOCATED | SRVOPEN_FLAG_FOBX_USED;
1813 }
1814
1815 /* If SrvOpen was properly allocated, initialize it */
1816 if (SrvOpen != NULL)
1817 {
1818 SrvOpen->Flags = Flags;
1819 SrvOpen->pFcb = RX_GET_MRX_FCB(Fcb);
1820 SrvOpen->pAlreadyPrefixedName = &Fcb->PrivateAlreadyPrefixedName;
1821 SrvOpen->pVNetRoot = (PMRX_V_NET_ROOT)VNetRoot;
1822 SrvOpen->ulFileSizeVersion = Fcb->ulFileSizeVersion;
1823 SrvOpen->NodeReferenceCount = 1;
1824
1825 RxReferenceVNetRoot(VNetRoot);
1826 RxReferenceNetFcb(Fcb);
1827
1828 InsertTailList(&Fcb->SrvOpenList, &SrvOpen->SrvOpenQLinks);
1829 ++Fcb->SrvOpenListVersion;
1830
1831 InitializeListHead(&SrvOpen->ScavengerFinalizationList);
1832 InitializeListHead(&SrvOpen->TransitionWaitList);
1833 InitializeListHead(&SrvOpen->FobxList);
1834 InitializeListHead(&SrvOpen->SrvOpenKeyList);
1835 }
1836 }
1837 _SEH2_FINALLY
1838 {
1839 if (_SEH2_AbnormalTermination())
1840 {
1841 if (SrvOpen != NULL)
1842 {
1843 RxFinalizeSrvOpen(SrvOpen, TRUE, TRUE);
1844 SrvOpen = NULL;
1845 }
1846 }
1847 else
1848 {
1849 DPRINT("SrvOpen %p for FCB %p\n", SrvOpen, SrvOpen->pFcb);
1850 }
1851 }
1852 _SEH2_END;
1853
1854 return SrvOpen;
1855 }
1856
1857 /*
1858 * @implemented
1859 */
1860 PV_NET_ROOT
1861 RxCreateVNetRoot(
1862 IN PRX_CONTEXT RxContext,
1863 IN PNET_ROOT NetRoot,
1864 IN PUNICODE_STRING CanonicalName,
1865 IN PUNICODE_STRING LocalNetRootName,
1866 IN PUNICODE_STRING FilePath,
1867 IN PRX_CONNECTION_ID RxConnectionId)
1868 {
1869 NTSTATUS Status;
1870 PV_NET_ROOT VNetRoot;
1871 USHORT CaseInsensitiveLength;
1872
1873 PAGED_CODE();
1874
1875 DPRINT("RxCreateVNetRoot(%p, %p, %wZ, %wZ, %wZ, %p)\n", RxContext, NetRoot, CanonicalName,
1876 LocalNetRootName, FilePath, RxConnectionId);
1877
1878 /* Lock must be held exclusively */
1879 ASSERT(RxIsPrefixTableLockExclusive(RxContext->RxDeviceObject->pRxNetNameTable));
1880
1881 /* Check for overflow */
1882 if (LocalNetRootName->Length + NetRoot->PrefixEntry.Prefix.Length > MAXUSHORT)
1883 {
1884 return NULL;
1885 }
1886
1887 /* Get name length and allocate VNetRoot */
1888 CaseInsensitiveLength = LocalNetRootName->Length + NetRoot->PrefixEntry.Prefix.Length;
1889 VNetRoot = RxAllocateObject(RDBSS_NTC_V_NETROOT, NetRoot->SrvCall->RxDeviceObject->Dispatch,
1890 CaseInsensitiveLength);
1891 if (VNetRoot == NULL)
1892 {
1893 return NULL;
1894 }
1895
1896 /* Initialize its connection parameters */
1897 Status = RxInitializeVNetRootParameters(RxContext, &VNetRoot->LogonId, &VNetRoot->SessionId,
1898 &VNetRoot->pUserName, &VNetRoot->pUserDomainName,
1899 &VNetRoot->pPassword, &VNetRoot->Flags);
1900 if (!NT_SUCCESS(Status))
1901 {
1902 RxUninitializeVNetRootParameters(VNetRoot->pUserName, VNetRoot->pUserDomainName,
1903 VNetRoot->pPassword, &VNetRoot->Flags);
1904 RxFreeObject(VNetRoot);
1905
1906 return NULL;
1907 }
1908
1909 /* Set name */
1910 RtlMoveMemory(VNetRoot->PrefixEntry.Prefix.Buffer, CanonicalName->Buffer, VNetRoot->PrefixEntry.Prefix.Length);
1911
1912 VNetRoot->PrefixOffsetInBytes = LocalNetRootName->Length + NetRoot->PrefixEntry.Prefix.Length;
1913 VNetRoot->NamePrefix.Buffer = Add2Ptr(VNetRoot->PrefixEntry.Prefix.Buffer, VNetRoot->PrefixOffsetInBytes);
1914 VNetRoot->NamePrefix.Length = VNetRoot->PrefixEntry.Prefix.Length - VNetRoot->PrefixOffsetInBytes;
1915 VNetRoot->NamePrefix.MaximumLength = VNetRoot->PrefixEntry.Prefix.Length - VNetRoot->PrefixOffsetInBytes;
1916
1917 InitializeListHead(&VNetRoot->TransitionWaitList);
1918 InitializeListHead(&VNetRoot->ScavengerFinalizationList);
1919
1920 if (!BooleanFlagOn(NetRoot->SrvCall->Flags, SRVCALL_FLAG_CASE_INSENSITIVE_FILENAMES))
1921 {
1922 USHORT i;
1923
1924 if (BooleanFlagOn(NetRoot->SrvCall->Flags, SRVCALL_FLAG_CASE_INSENSITIVE_NETROOTS))
1925 {
1926 CaseInsensitiveLength = NetRoot->PrefixEntry.CaseInsensitiveLength;
1927 }
1928 else
1929 {
1930 CaseInsensitiveLength = NetRoot->SrvCall->PrefixEntry.CaseInsensitiveLength;
1931 }
1932
1933 for (i = 1; i < CanonicalName->Length / sizeof(WCHAR); ++i)
1934 {
1935 if (CanonicalName->Buffer[i] != OBJ_NAME_PATH_SEPARATOR)
1936 {
1937 break;
1938 }
1939 }
1940
1941 CaseInsensitiveLength += (i * sizeof(WCHAR));
1942 }
1943
1944 /* Insert in prefix table */
1945 RxPrefixTableInsertName(RxContext->RxDeviceObject->pRxNetNameTable, &VNetRoot->PrefixEntry,
1946 VNetRoot, (PULONG)&VNetRoot->NodeReferenceCount, CaseInsensitiveLength,
1947 RxConnectionId);
1948
1949 RxReferenceNetRoot(NetRoot);
1950 RxAddVirtualNetRootToNetRoot(NetRoot, VNetRoot);
1951
1952 /* Finish init */
1953 VNetRoot->SerialNumberForEnum = SerialNumber++;
1954 VNetRoot->UpperFinalizationDone = FALSE;
1955 VNetRoot->ConnectionFinalizationDone = FALSE;
1956 VNetRoot->AdditionalReferenceForDeleteFsctlTaken = 0;
1957
1958 DPRINT("NamePrefix: %wZ\n", &VNetRoot->NamePrefix);
1959 DPRINT("PrefixEntry: %wZ\n", &VNetRoot->PrefixEntry.Prefix);
1960
1961 return VNetRoot;
1962 }
1963
1964 /*
1965 * @implemented
1966 */
1967 VOID
1968 RxDereference(
1969 IN OUT PVOID Instance,
1970 IN LOCK_HOLDING_STATE LockHoldingState)
1971 {
1972 LONG RefCount;
1973 NODE_TYPE_CODE NodeType;
1974 PNODE_TYPE_AND_SIZE Node;
1975
1976 PAGED_CODE();
1977
1978 RxAcquireScavengerMutex();
1979
1980 /* Check we have a node we can handle */
1981 NodeType = NodeType(Instance);
1982 ASSERT((NodeType == RDBSS_NTC_SRVCALL) || (NodeType == RDBSS_NTC_NETROOT) ||
1983 (NodeType == RDBSS_NTC_V_NETROOT) || (NodeType == RDBSS_NTC_SRVOPEN) ||
1984 (NodeType == RDBSS_NTC_FOBX));
1985
1986 Node = (PNODE_TYPE_AND_SIZE)Instance;
1987 RefCount = InterlockedDecrement((volatile long *)&Node->NodeReferenceCount);
1988 ASSERT(RefCount >= 0);
1989
1990 /* Trace refcount */
1991 switch (NodeType)
1992 {
1993 case RDBSS_NTC_SRVCALL:
1994 PRINT_REF_COUNT(SRVCALL, Node->NodeReferenceCount);
1995 break;
1996
1997 case RDBSS_NTC_NETROOT:
1998 PRINT_REF_COUNT(NETROOT, Node->NodeReferenceCount);
1999 break;
2000
2001 case RDBSS_NTC_V_NETROOT:
2002 PRINT_REF_COUNT(VNETROOT, Node->NodeReferenceCount);
2003 break;
2004
2005 case RDBSS_NTC_SRVOPEN:
2006 PRINT_REF_COUNT(SRVOPEN, Node->NodeReferenceCount);
2007 break;
2008
2009 case RDBSS_NTC_FOBX:
2010 PRINT_REF_COUNT(NETFOBX, Node->NodeReferenceCount);
2011 break;
2012
2013 default:
2014 ASSERT(FALSE);
2015 break;
2016 }
2017
2018 /* No need to free - still in use */
2019 if (RefCount > 1)
2020 {
2021 RxReleaseScavengerMutex();
2022 return;
2023 }
2024
2025 /* We have to be locked exclusively */
2026 if (LockHoldingState != LHS_ExclusiveLockHeld)
2027 {
2028 if ((NodeType == RDBSS_NTC_FOBX && RefCount == 0) ||
2029 (NodeType >= RDBSS_NTC_SRVCALL && NodeType <= RDBSS_NTC_V_NETROOT))
2030 {
2031 RxpMarkInstanceForScavengedFinalization(Instance);
2032 }
2033
2034 RxReleaseScavengerMutex();
2035 return;
2036 }
2037 else
2038 {
2039 if (BooleanFlagOn(NodeType, RX_SCAVENGER_MASK))
2040 {
2041 RxpUndoScavengerFinalizationMarking(Instance);
2042 }
2043 }
2044
2045 RxReleaseScavengerMutex();
2046
2047 /* Now, deallocate the memory */
2048 switch (NodeType)
2049 {
2050 case RDBSS_NTC_SRVCALL:
2051 {
2052 PSRV_CALL SrvCall;
2053
2054 SrvCall = (PSRV_CALL)Instance;
2055
2056 ASSERT(SrvCall->RxDeviceObject != NULL);
2057 ASSERT(RxIsPrefixTableLockAcquired(SrvCall->RxDeviceObject->pRxNetNameTable));
2058 RxFinalizeSrvCall(SrvCall, TRUE, TRUE);
2059 break;
2060 }
2061
2062 case RDBSS_NTC_NETROOT:
2063 {
2064 PNET_ROOT NetRoot;
2065
2066 NetRoot = (PNET_ROOT)Instance;
2067
2068 ASSERT(NetRoot->pSrvCall->RxDeviceObject != NULL);
2069 ASSERT(RxIsPrefixTableLockAcquired(NetRoot->pSrvCall->RxDeviceObject->pRxNetNameTable));
2070 RxFinalizeNetRoot(NetRoot, TRUE, TRUE);
2071 break;
2072 }
2073
2074 case RDBSS_NTC_V_NETROOT:
2075 {
2076 PV_NET_ROOT VNetRoot;
2077
2078 VNetRoot = (PV_NET_ROOT)Instance;
2079
2080 ASSERT(VNetRoot->pNetRoot->pSrvCall->RxDeviceObject != NULL);
2081 ASSERT(RxIsPrefixTableLockAcquired(VNetRoot->pNetRoot->pSrvCall->RxDeviceObject->pRxNetNameTable));
2082 RxFinalizeVNetRoot(VNetRoot, TRUE, TRUE);
2083 break;
2084 }
2085
2086 case RDBSS_NTC_SRVOPEN:
2087 {
2088 PSRV_OPEN SrvOpen;
2089
2090 SrvOpen = (PSRV_OPEN)Instance;
2091
2092 ASSERT(RxIsFcbAcquired(SrvOpen->Fcb));
2093 if (SrvOpen->OpenCount == 0)
2094 {
2095 RxFinalizeSrvOpen(SrvOpen, FALSE, FALSE);
2096 }
2097 break;
2098 }
2099
2100 case RDBSS_NTC_FOBX:
2101 {
2102 PFOBX Fobx;
2103
2104 Fobx = (PFOBX)Instance;
2105
2106 ASSERT(RxIsFcbAcquired(Fobx->SrvOpen->Fcb));
2107 RxFinalizeNetFobx(Fobx, TRUE, FALSE);
2108 break;
2109 }
2110 }
2111 }
2112
2113 /*
2114 * @implemented
2115 */
2116 VOID
2117 NTAPI
2118 RxDereferenceAndDeleteRxContext_Real(
2119 IN PRX_CONTEXT RxContext)
2120 {
2121 KIRQL OldIrql;
2122 ULONG RefCount;
2123 BOOLEAN Allocated;
2124 PRX_CONTEXT StopContext = NULL;
2125
2126 /* Make sure we really have a context */
2127 KeAcquireSpinLock(&RxStrucSupSpinLock, &OldIrql);
2128 ASSERT(RxContext->NodeTypeCode == RDBSS_NTC_RX_CONTEXT);
2129 RefCount = InterlockedDecrement((volatile LONG *)&RxContext->ReferenceCount);
2130 /* If refcount is 0, start releasing stuff that needs spinlock held */
2131 if (RefCount == 0)
2132 {
2133 PRDBSS_DEVICE_OBJECT RxDeviceObject;
2134
2135 Allocated = BooleanFlagOn(RxContext->Flags, RX_CONTEXT_FLAG_FROM_POOL);
2136
2137 /* If that's stop context from DO, remove it */
2138 RxDeviceObject = RxContext->RxDeviceObject;
2139 if (RxDeviceObject->StartStopContext.pStopContext == RxContext)
2140 {
2141 RxDeviceObject->StartStopContext.pStopContext = NULL;
2142 }
2143 else
2144 {
2145 /* Remove it from the list */
2146 ASSERT((RxContext->ContextListEntry.Flink->Blink == &RxContext->ContextListEntry) &&
2147 (RxContext->ContextListEntry.Blink->Flink == &RxContext->ContextListEntry));
2148 RemoveEntryList(&RxContext->ContextListEntry);
2149
2150 /* If that was the last active context, save the stop context */
2151 if (InterlockedExchangeAdd((volatile LONG *)&RxDeviceObject->NumberOfActiveContexts, -1) == 0)
2152 {
2153 if (RxDeviceObject->StartStopContext.pStopContext != NULL)
2154 {
2155 StopContext = RxDeviceObject->StartStopContext.pStopContext;
2156 }
2157 }
2158 }
2159 }
2160 KeReleaseSpinLock(&RxStrucSupSpinLock, OldIrql);
2161
2162 /* Now, deal with what can be done without spinlock held */
2163 if (RefCount == 0)
2164 {
2165 /* Refcount shouldn't have changed */
2166 ASSERT(RxContext->ReferenceCount == 0);
2167 /* Reset everything that can be */
2168 RxPrepareContextForReuse(RxContext);
2169
2170 #ifdef RDBSS_TRACKER
2171 ASSERT(RxContext->AcquireReleaseFcbTrackerX == 0);
2172 #endif
2173 /* If that was the last active, set the event */
2174 if (StopContext != NULL)
2175 {
2176 StopContext->Flags &= ~RX_CONTEXT_FLAG_RECURSIVE_CALL;
2177 KeSetEvent(&StopContext->SyncEvent, IO_NO_INCREMENT, FALSE);
2178 }
2179
2180 /* Is ShadowCrit still owned? Shouldn't happen! */
2181 if (RxContext->ShadowCritOwner != 0)
2182 {
2183 DPRINT1("ShadowCritOwner not null! %p\n", (PVOID)RxContext->ShadowCritOwner);
2184 ASSERT(FALSE);
2185 }
2186
2187 /* If it was allocated, free it */
2188 if (Allocated)
2189 {
2190 ExFreeToNPagedLookasideList(&RxContextLookasideList, RxContext);
2191 }
2192 }
2193 }
2194
2195 VOID
2196 NTAPI
2197 RxDispatchChangeBufferingStateRequests(
2198 PVOID Context)
2199 {
2200 UNIMPLEMENTED;
2201 }
2202
2203 /*
2204 * @implemented
2205 */
2206 NTSTATUS
2207 NTAPI
2208 RxDispatchToWorkerThread(
2209 IN PRDBSS_DEVICE_OBJECT pMRxDeviceObject,
2210 IN WORK_QUEUE_TYPE WorkQueueType,
2211 IN PRX_WORKERTHREAD_ROUTINE Routine,
2212 IN PVOID pContext)
2213 {
2214 NTSTATUS Status;
2215 PRX_WORK_DISPATCH_ITEM DispatchItem;
2216
2217 /* Allocate a bit of context */
2218 DispatchItem = RxAllocatePoolWithTag(PagedPool, sizeof(RX_WORK_DISPATCH_ITEM), RX_WORKQ_POOLTAG);
2219 if (DispatchItem == NULL)
2220 {
2221 return STATUS_INSUFFICIENT_RESOURCES;
2222 }
2223
2224 /* Set all the routines, the one our dispatcher will call, the one ntoskrnl will call */
2225 DispatchItem->DispatchRoutine = Routine;
2226 DispatchItem->DispatchRoutineParameter = pContext;
2227 DispatchItem->WorkQueueItem.WorkerRoutine = RxWorkItemDispatcher;
2228 DispatchItem->WorkQueueItem.Parameter = DispatchItem;
2229
2230 /* Insert item */
2231 Status = RxInsertWorkQueueItem(pMRxDeviceObject, WorkQueueType, &DispatchItem->WorkQueueItem);
2232 if (!NT_SUCCESS(Status))
2233 {
2234 RxFreePoolWithTag(DispatchItem, RX_WORKQ_POOLTAG);
2235 DPRINT1("RxInsertWorkQueueItem failed! Queue: %ld, Routine: %p, Context: %p, Status: %lx\n", WorkQueueType, Routine, pContext, Status);
2236 }
2237
2238 DPRINT("Dispatching: %p, %p\n", Routine, pContext);
2239
2240 return Status;
2241 }
2242
2243 /*
2244 * @implemented
2245 */
2246 VOID
2247 RxExclusivePrefixTableLockToShared(
2248 PRX_PREFIX_TABLE Table)
2249 {
2250 PAGED_CODE();
2251
2252 ExConvertExclusiveToSharedLite(&Table->TableLock);
2253 }
2254
2255 /*
2256 * @implemented
2257 */
2258 VOID
2259 RxExtractServerName(
2260 IN PUNICODE_STRING FilePathName,
2261 OUT PUNICODE_STRING SrvCallName,
2262 OUT PUNICODE_STRING RestOfName)
2263 {
2264 USHORT i, Length;
2265
2266 PAGED_CODE();
2267
2268 ASSERT(SrvCallName != NULL);
2269
2270 /* SrvCall name will start from the begin up to the first separator */
2271 SrvCallName->Buffer = FilePathName->Buffer;
2272 for (i = 1; i < FilePathName->Length / sizeof(WCHAR); ++i)
2273 {
2274 if (FilePathName->Buffer[i] == OBJ_NAME_PATH_SEPARATOR)
2275 {
2276 break;
2277 }
2278 }
2279
2280 /* Compute length */
2281 Length = (USHORT)((ULONG_PTR)&FilePathName->Buffer[i] - (ULONG_PTR)FilePathName->Buffer);
2282 SrvCallName->MaximumLength = Length;
2283 SrvCallName->Length = Length;
2284
2285 /* Return the rest if asked */
2286 if (RestOfName != NULL)
2287 {
2288 Length = (USHORT)((ULONG_PTR)&FilePathName->Buffer[FilePathName->Length / sizeof(WCHAR)] - (ULONG_PTR)FilePathName->Buffer[i]);
2289 RestOfName->Buffer = &FilePathName->Buffer[i];
2290 RestOfName->MaximumLength = Length;
2291 RestOfName->Length = Length;
2292 }
2293 }
2294
2295 /*
2296 * @implemented
2297 */
2298 NTSTATUS
2299 RxFcbTableInsertFcb(
2300 IN OUT PRX_FCB_TABLE FcbTable,
2301 IN OUT PFCB Fcb)
2302 {
2303 PAGED_CODE();
2304
2305 /* We deal with the table, make sure it's locked */
2306 ASSERT(RxIsFcbTableLockExclusive(FcbTable));
2307
2308 /* Compute the hash */
2309 Fcb->FcbTableEntry.HashValue = RxTableComputePathHashValue(&Fcb->FcbTableEntry.Path);
2310
2311 RxReferenceNetFcb(Fcb);
2312
2313 /* If no length, it will be our null entry */
2314 if (Fcb->FcbTableEntry.Path.Length == 0)
2315 {
2316 FcbTable->TableEntryForNull = &Fcb->FcbTableEntry;
2317 }
2318 /* Otherwise, insert in the appropriate bucket */
2319 else
2320 {
2321 InsertTailList(FCB_HASH_BUCKET(FcbTable, Fcb->FcbTableEntry.HashValue),
2322 &Fcb->FcbTableEntry.HashLinks);
2323 }
2324
2325 /* Propagate the change by incrementing the version number */
2326 InterlockedIncrement((volatile long *)&FcbTable->Version);
2327
2328 return STATUS_SUCCESS;
2329 }
2330
2331 /*
2332 * @implemented
2333 */
2334 PFCB
2335 RxFcbTableLookupFcb(
2336 IN PRX_FCB_TABLE FcbTable,
2337 IN PUNICODE_STRING Path)
2338 {
2339 PFCB Fcb;
2340 PRX_FCB_TABLE_ENTRY TableEntry;
2341
2342 PAGED_CODE();
2343
2344 /* No path - easy, that's null entry */
2345 if (Path == NULL)
2346 {
2347 TableEntry = FcbTable->TableEntryForNull;
2348 }
2349 else
2350 {
2351 ULONG Hash;
2352 PLIST_ENTRY HashBucket, ListEntry;
2353
2354 /* Otherwise, compute the hash value and find the associated bucket */
2355 Hash = RxTableComputePathHashValue(Path);
2356 HashBucket = FCB_HASH_BUCKET(FcbTable, Hash);
2357 /* If the bucket is empty, it means there's no entry yet */
2358 if (IsListEmpty(HashBucket))
2359 {
2360 TableEntry = NULL;
2361 }
2362 else
2363 {
2364 /* Otherwise, browse all the entry */
2365 for (ListEntry = HashBucket->Flink;
2366 ListEntry != HashBucket;
2367 ListEntry = ListEntry->Flink)
2368 {
2369 TableEntry = CONTAINING_RECORD(ListEntry, RX_FCB_TABLE_ENTRY, HashLinks);
2370 InterlockedIncrement(&FcbTable->Compares);
2371
2372 /* If entry hash and string are equal, thatt's the one! */
2373 if (TableEntry->HashValue == Hash &&
2374 TableEntry->Path.Length == Path->Length &&
2375 RtlEqualUnicodeString(Path, &TableEntry->Path, FcbTable->CaseInsensitiveMatch))
2376 {
2377 break;
2378 }
2379 }
2380
2381 /* We reached the end? Not found */
2382 if (ListEntry == HashBucket)
2383 {
2384 TableEntry = NULL;
2385 }
2386 }
2387 }
2388
2389 InterlockedIncrement(&FcbTable->Lookups);
2390
2391 /* If table entry isn't null, return the FCB */
2392 if (TableEntry != NULL)
2393 {
2394 Fcb = CONTAINING_RECORD(TableEntry, FCB, FcbTableEntry);
2395 RxReferenceNetFcb(Fcb);
2396 }
2397 else
2398 {
2399 Fcb = NULL;
2400 InterlockedIncrement(&FcbTable->FailedLookups);
2401 }
2402
2403 return Fcb;
2404 }
2405
2406 /*
2407 * @implemented
2408 */
2409 NTSTATUS
2410 RxFcbTableRemoveFcb(
2411 IN OUT PRX_FCB_TABLE FcbTable,
2412 IN OUT PFCB Fcb)
2413 {
2414 PAGED_CODE();
2415
2416 ASSERT(RxIsPrefixTableLockExclusive(FcbTable));
2417
2418 /* If no path, then remove entry for null */
2419 if (Fcb->FcbTableEntry.Path.Length == 0)
2420 {
2421 FcbTable->TableEntryForNull = NULL;
2422 }
2423 /* Otherwise, remove from the bucket */
2424 else
2425 {
2426 RemoveEntryList(&Fcb->FcbTableEntry.HashLinks);
2427 }
2428
2429 /* Reset its list entry */
2430 InitializeListHead(&Fcb->FcbTableEntry.HashLinks);
2431
2432 /* Propagate the change by incrementing the version number */
2433 InterlockedIncrement((volatile long *)&FcbTable->Version);
2434
2435 return STATUS_SUCCESS;
2436 }
2437
2438 /*
2439 * @implemented
2440 */
2441 VOID
2442 RxFinalizeFcbTable(
2443 IN OUT PRX_FCB_TABLE FcbTable)
2444 {
2445 USHORT Bucket;
2446
2447 PAGED_CODE();
2448
2449 /* Just delete the lock */
2450 ExDeleteResourceLite(&FcbTable->TableLock);
2451
2452 /* And make sure (checked) that the table is really empty... */
2453 for (Bucket = 0; Bucket < FcbTable->NumberOfBuckets; ++Bucket)
2454 {
2455 ASSERT(IsListEmpty(&FcbTable->HashBuckets[Bucket]));
2456 }
2457 }
2458
2459 /*
2460 * @implemented
2461 */
2462 BOOLEAN
2463 RxFinalizeNetFcb(
2464 OUT PFCB ThisFcb,
2465 IN BOOLEAN RecursiveFinalize,
2466 IN BOOLEAN ForceFinalize,
2467 IN LONG ReferenceCount)
2468 {
2469 PAGED_CODE();
2470
2471 DPRINT("RxFinalizeNetFcb(%p, %d, %d, %d)\n", ThisFcb, RecursiveFinalize, ForceFinalize, ReferenceCount);
2472 DPRINT("Finalize: %wZ\n", &ThisFcb->FcbTableEntry.Path);
2473
2474 /* Make sure we have an exclusively acquired FCB */
2475 ASSERT_CORRECT_FCB_STRUCTURE(ThisFcb);
2476 ASSERT(RxIsFcbAcquiredExclusive(ThisFcb));
2477
2478 /* We shouldn't force finalization... */
2479 ASSERT(!ForceFinalize);
2480
2481 /* If recurisve, finalize all the associated SRV_OPEN */
2482 if (RecursiveFinalize)
2483 {
2484 PLIST_ENTRY ListEntry;
2485
2486 for (ListEntry = ThisFcb->SrvOpenList.Flink;
2487 ListEntry != &ThisFcb->SrvOpenList;
2488 ListEntry = ListEntry->Flink)
2489 {
2490 PSRV_OPEN SrvOpen;
2491
2492 SrvOpen = CONTAINING_RECORD(ListEntry, SRV_OPEN, SrvOpenQLinks);
2493 RxFinalizeSrvOpen(SrvOpen, TRUE, ForceFinalize);
2494 }
2495 }
2496 /* If FCB is still in use, that's over */
2497 else
2498 {
2499 if (ThisFcb->OpenCount != 0 || ThisFcb->UncleanCount != 0)
2500 {
2501 ASSERT(ReferenceCount > 0);
2502
2503 return FALSE;
2504 }
2505 }
2506
2507 ASSERT(ReferenceCount >= 1);
2508
2509 /* If FCB is still referenced, that's over - unless you force it and want to BSOD somewhere */
2510 if (ReferenceCount != 1 && !ForceFinalize)
2511 {
2512 return FALSE;
2513 }
2514
2515 ASSERT(ForceFinalize || ((ThisFcb->OpenCount == 0) && (ThisFcb->UncleanCount == 0)));
2516
2517 DPRINT("Finalizing FCB open: %d (%d)\n", ThisFcb->OpenCount, ForceFinalize);
2518
2519 /* If finalization was not already initiated, go ahead */
2520 if (!ThisFcb->UpperFinalizationDone)
2521 {
2522 /* Free any FCB_LOCK */
2523 if (NodeType(ThisFcb) == RDBSS_NTC_STORAGE_TYPE_FILE)
2524 {
2525 FsRtlUninitializeFileLock(&ThisFcb->Specific.Fcb.FileLock);
2526
2527 while (ThisFcb->BufferedLocks.List != NULL)
2528 {
2529 PFCB_LOCK Entry;
2530
2531 Entry = ThisFcb->BufferedLocks.List;
2532 ThisFcb->BufferedLocks.List = Entry->Next;
2533
2534 RxFreePool(Entry);
2535 }
2536 }
2537
2538 /* If not orphaned, it still has a NET_ROOT and potentially is still in a table */
2539 if (!BooleanFlagOn(ThisFcb->FcbState, FCB_STATE_ORPHANED))
2540 {
2541 PNET_ROOT NetRoot;
2542
2543 NetRoot = (PNET_ROOT)ThisFcb->pNetRoot;
2544
2545 ASSERT(RxIsFcbTableLockExclusive(&NetRoot->FcbTable));
2546 /* So, remove it */
2547 if (!BooleanFlagOn(ThisFcb->FcbState, FCB_STATE_NAME_ALREADY_REMOVED))
2548 {
2549 RxFcbTableRemoveFcb(&NetRoot->FcbTable, ThisFcb);
2550 }
2551 }
2552
2553 ThisFcb->UpperFinalizationDone = TRUE;
2554 }
2555
2556 ASSERT(ReferenceCount >= 1);
2557
2558 /* Even if forced, don't allow broken free */
2559 if (ReferenceCount != 1)
2560 {
2561 return FALSE;
2562 }
2563
2564 /* Now, release everything */
2565 if (ThisFcb->pBufferingStateChangeCompletedEvent != NULL)
2566 {
2567 RxFreePool(ThisFcb->pBufferingStateChangeCompletedEvent);
2568 }
2569
2570 if (ThisFcb->MRxDispatch != NULL)
2571 {
2572 ThisFcb->MRxDispatch->MRxDeallocateForFcb(RX_GET_MRX_FCB(ThisFcb));
2573 }
2574
2575 ExDeleteResourceLite(ThisFcb->BufferedLocks.Resource);
2576 ExDeleteResourceLite(ThisFcb->Header.Resource);
2577 ExDeleteResourceLite(ThisFcb->Header.PagingIoResource);
2578
2579 InterlockedDecrement((volatile long *)&ThisFcb->pNetRoot->NumberOfFcbs);
2580 RxDereferenceVNetRoot(ThisFcb->VNetRoot, LHS_LockNotHeld);
2581
2582 ASSERT(IsListEmpty(&ThisFcb->FcbTableEntry.HashLinks));
2583 ASSERT(!ThisFcb->fMiniInited);
2584
2585 /* And free the object */
2586 RxFreeFcbObject(ThisFcb);
2587
2588 return TRUE;
2589 }
2590
2591 /*
2592 * @implemented
2593 */
2594 BOOLEAN
2595 RxFinalizeNetFobx(
2596 _Out_ PFOBX ThisFobx,
2597 _In_ BOOLEAN RecursiveFinalize,
2598 _In_ BOOLEAN ForceFinalize)
2599 {
2600 PFCB Fcb;
2601 PSRV_OPEN SrvOpen;
2602
2603 PAGED_CODE();
2604
2605 ASSERT(NodeType(ThisFobx) == RDBSS_NTC_FOBX);
2606
2607 /* Only finalize if forced or if there's no ref left */
2608 if (ThisFobx->NodeReferenceCount != 0 &&
2609 !ForceFinalize)
2610 {
2611 return FALSE;
2612 }
2613
2614 DPRINT("Finalize Fobx: %p (with %d ref), forced: %d\n", ThisFobx, ThisFobx->NodeReferenceCount, ForceFinalize);
2615
2616 SrvOpen = ThisFobx->SrvOpen;
2617 Fcb = SrvOpen->Fcb;
2618 /* If it wasn't finalized yet, do it */
2619 if (!ThisFobx->UpperFinalizationDone)
2620 {
2621 ASSERT(NodeType(SrvOpen->Fcb) != RDBSS_NTC_OPENTARGETDIR_FCB);
2622 ASSERT(RxIsFcbAcquiredExclusive(SrvOpen->Fcb));
2623
2624 /* Remove it from the SRV_OPEN */
2625 RemoveEntryList(&ThisFobx->FobxQLinks);
2626
2627 /* If we were used to browse a directory, free the query buffer */
2628 if (BooleanFlagOn(ThisFobx->Flags, FOBX_FLAG_FREE_UNICODE))
2629 {
2630 RxFreePoolWithTag(ThisFobx->UnicodeQueryTemplate.Buffer, RX_DIRCTL_POOLTAG);
2631 }
2632
2633 /* Notify the mini-rdr */
2634 if (Fcb->MRxDispatch != NULL && Fcb->MRxDispatch->MRxDeallocateForFobx != NULL)
2635 {
2636 Fcb->MRxDispatch->MRxDeallocateForFobx((PMRX_FOBX)ThisFobx);
2637 }
2638
2639 /* If the SRV_OPEN wasn't closed yet, do it */
2640 if (!BooleanFlagOn(ThisFobx->Flags, FOBX_FLAG_SRVOPEN_CLOSED))
2641 {
2642 NTSTATUS Status;
2643
2644 Status = RxCloseAssociatedSrvOpen(ThisFobx, FALSE);
2645 DPRINT("Closing SRV_OPEN %p for %p: %x\n", SrvOpen, ThisFobx, Status);
2646 }
2647
2648 /* Finalization done */
2649 ThisFobx->UpperFinalizationDone = TRUE;
2650 }
2651
2652 /* If we're still referenced, don't go any further! */
2653 if (ThisFobx->NodeReferenceCount != 0)
2654 {
2655 return FALSE;
2656 }
2657
2658 /* At that point, everything should be closed */
2659 ASSERT(IsListEmpty(&ThisFobx->ClosePendingList));
2660
2661 /* Was the FOBX allocated with another object?
2662 * If so, mark the buffer free in said object
2663 */
2664 if (ThisFobx == Fcb->InternalFobx)
2665 {
2666 ClearFlag(Fcb->FcbState, FCB_STATE_FOBX_USED);
2667 }
2668 else if (ThisFobx == SrvOpen->InternalFobx)
2669 {
2670 ClearFlag(SrvOpen->Flags, SRVOPEN_FLAG_FOBX_USED);
2671 }
2672
2673 ThisFobx->pSrvOpen = NULL;
2674
2675 /* A FOBX less */
2676 InterlockedDecrement((volatile long *)&SrvOpen->pVNetRoot->NumberOfFobxs);
2677
2678 RxDereferenceSrvOpen(SrvOpen, LHS_ExclusiveLockHeld);
2679
2680 /* If it wasn't allocated with another object, free the FOBX */
2681 if (!BooleanFlagOn(ThisFobx->Flags, FOBX_FLAG_ENCLOSED_ALLOCATED))
2682 {
2683 RxFreeFcbObject(ThisFobx);
2684 }
2685
2686 return TRUE;
2687 }
2688
2689 /*
2690 * @implemented
2691 */
2692 BOOLEAN
2693 RxFinalizeNetRoot(
2694 OUT PNET_ROOT ThisNetRoot,
2695 IN BOOLEAN RecursiveFinalize,
2696 IN BOOLEAN ForceFinalize)
2697 {
2698 PSRV_CALL SrvCall;
2699 PRX_FCB_TABLE FcbTable;
2700 PRX_PREFIX_TABLE PrefixTable;
2701
2702 PAGED_CODE();
2703
2704 ASSERT(NodeType(ThisNetRoot) == RDBSS_NTC_NETROOT);
2705
2706 PrefixTable = ThisNetRoot->pSrvCall->RxDeviceObject->pRxNetNameTable;
2707 ASSERT(RxIsPrefixTableLockAcquired(PrefixTable));
2708
2709 /* If sme finalization is already ongoing, leave */
2710 if (BooleanFlagOn(ThisNetRoot->Flags, NETROOT_FLAG_FINALIZATION_IN_PROGRESS))
2711 {
2712 return FALSE;
2713 }
2714
2715 /* Mark we're finalizing */
2716 SetFlag(ThisNetRoot->Flags, NETROOT_FLAG_FINALIZATION_IN_PROGRESS);
2717
2718 FcbTable = &ThisNetRoot->FcbTable;
2719 /* Did caller asked us to finalize any associated FCB? */
2720 if (RecursiveFinalize)
2721 {
2722 USHORT Bucket;
2723
2724 /* Browse all the FCBs in our FCB table */
2725 RxAcquireFcbTableLockExclusive(FcbTable, TRUE);
2726 for (Bucket = 0; Bucket < FcbTable->NumberOfBuckets; ++Bucket)
2727 {
2728 PLIST_ENTRY HashBucket, ListEntry;
2729
2730 HashBucket = &FcbTable->HashBuckets[Bucket];
2731 ListEntry = HashBucket->Flink;
2732 while (ListEntry != HashBucket)
2733 {
2734 PFCB Fcb;
2735
2736 Fcb = CONTAINING_RECORD(ListEntry, FCB, FcbTableEntry.HashLinks);
2737 ASSERT(NodeTypeIsFcb(Fcb));
2738
2739 ListEntry = ListEntry->Flink;
2740
2741 /* If the FCB isn't orphaned, then, it's time to purge it */
2742 if (!BooleanFlagOn(Fcb->FcbState, FCB_STATE_ORPHANED))
2743 {
2744 NTSTATUS Status;
2745
2746 Status = RxAcquireExclusiveFcb(NULL, Fcb);
2747 ASSERT(Status == STATUS_SUCCESS);
2748 RxPurgeFcb(Fcb);
2749 }
2750 }
2751 }
2752 RxReleaseFcbTableLock(FcbTable);
2753 }
2754
2755 /* Only finalize if forced or if there's a single ref left */
2756 if (ThisNetRoot->NodeReferenceCount != 1 && !ForceFinalize)
2757 {
2758 return FALSE;
2759 }
2760
2761 DPRINT("Finalizing NetRoot %p for %wZ\n", ThisNetRoot, &ThisNetRoot->PrefixEntry.Prefix);
2762
2763 /* If we're still referenced, don't go any further! */
2764 if (ThisNetRoot->NodeReferenceCount != 1)
2765 {
2766 return FALSE;
2767 }
2768
2769 /* Finalize the FCB table (and make sure it's empty!) */
2770 RxFinalizeFcbTable(FcbTable);
2771
2772 /* If name wasn't remove already, do it now */
2773 if (!BooleanFlagOn(ThisNetRoot->Flags, NETROOT_FLAG_NAME_ALREADY_REMOVED))
2774 {
2775 RxRemovePrefixTableEntry(PrefixTable, &ThisNetRoot->PrefixEntry);
2776 }
2777
2778 /* Delete the object */
2779 SrvCall = (PSRV_CALL)ThisNetRoot->pSrvCall;
2780 RxFreeObject(ThisNetRoot);
2781
2782 /* And dereference the associated SRV_CALL */
2783 if (SrvCall != NULL)
2784 {
2785 RxDereferenceSrvCall(SrvCall, LHS_ExclusiveLockHeld);
2786 }
2787
2788 return TRUE;
2789 }
2790
2791 /*
2792 * @implemented
2793 */
2794 BOOLEAN
2795 RxFinalizeSrvCall(
2796 OUT PSRV_CALL ThisSrvCall,
2797 IN BOOLEAN RecursiveFinalize,
2798 IN BOOLEAN ForceFinalize)
2799 {
2800 PRX_PREFIX_TABLE PrefixTable;
2801
2802 PAGED_CODE();
2803
2804 ASSERT(NodeType(ThisSrvCall) == RDBSS_NTC_SRVCALL);
2805
2806 PrefixTable = ThisSrvCall->RxDeviceObject->pRxNetNameTable;
2807 ASSERT(RxIsPrefixTableLockAcquired(PrefixTable));
2808
2809 /* Only finalize if forced or if there's a single ref left */
2810 if (ThisSrvCall->NodeReferenceCount != 1 &&
2811 !ForceFinalize)
2812 {
2813 return FALSE;
2814 }
2815
2816 DPRINT("Finalizing SrvCall %p for %wZ\n", ThisSrvCall, &ThisSrvCall->PrefixEntry.Prefix);
2817
2818 /* If it wasn't finalized yet, do it */
2819 if (!ThisSrvCall->UpperFinalizationDone)
2820 {
2821 BOOLEAN WillFree;
2822
2823 /* Remove ourselves from prefix table */
2824 RxRemovePrefixTableEntry(PrefixTable, &ThisSrvCall->PrefixEntry);
2825
2826 /* Remember our third arg, in case we get queued for later execution */
2827 if (ForceFinalize)
2828 {
2829 SetFlag(ThisSrvCall->Flags, SRVCALL_FLAG_FORCE_FINALIZED);
2830 }
2831
2832 /* And done */
2833 ThisSrvCall->UpperFinalizationDone = TRUE;
2834
2835 /* Would defered execution free the object? */
2836 WillFree = (ThisSrvCall->NodeReferenceCount == 1);
2837
2838 /* If we have a device object */
2839 if (ThisSrvCall->RxDeviceObject != NULL)
2840 {
2841 NTSTATUS Status;
2842
2843 /* If we're not executing in the RDBSS thread, queue for execution within the thread */
2844 if (RxGetRDBSSProcess() != IoGetCurrentProcess())
2845 {
2846 /* Extra ref, as usual */
2847 InterlockedIncrement((volatile long *)&ThisSrvCall->NodeReferenceCount);
2848 /* And dispatch */
2849 RxDispatchToWorkerThread(ThisSrvCall->RxDeviceObject, DelayedWorkQueue, RxpDestroySrvCall, ThisSrvCall);
2850
2851 /* Return to the caller, in advance, whether we're freeing the object or not */
2852 return WillFree;
2853 }
2854
2855 /* If in the right thread already, call the mini-rdr */
2856 MINIRDR_CALL_THROUGH(Status, ThisSrvCall->RxDeviceObject->Dispatch,
2857 MRxFinalizeSrvCall, ((PMRX_SRV_CALL)ThisSrvCall, ForceFinalize));
2858 (void)Status;
2859 }
2860 }
2861
2862 /* If we're still referenced, don't go any further! */
2863 if (ThisSrvCall->NodeReferenceCount != 1)
2864 {
2865 return FALSE;
2866 }
2867
2868 /* Don't leak */
2869 if (ThisSrvCall->pDomainName != NULL)
2870 {
2871 RxFreePool(ThisSrvCall->pDomainName);
2872 }
2873
2874 /* And free! */
2875 RxTearDownBufferingManager(ThisSrvCall);
2876 RxFreeObject(ThisSrvCall);
2877
2878 return TRUE;
2879 }
2880
2881 /*
2882 * @implemented
2883 */
2884 BOOLEAN
2885 RxFinalizeSrvOpen(
2886 OUT PSRV_OPEN ThisSrvOpen,
2887 IN BOOLEAN RecursiveFinalize,
2888 IN BOOLEAN ForceFinalize)
2889 {
2890 PFCB Fcb;
2891
2892 PAGED_CODE();
2893
2894 /* We have to have a SRV_OPEN */
2895 ASSERT(NodeType(ThisSrvOpen) == RDBSS_NTC_SRVOPEN);
2896
2897 /* If that's a recursive finalization, finalize any related FOBX */
2898 if (RecursiveFinalize)
2899 {
2900 PLIST_ENTRY ListEntry;
2901
2902 ListEntry = ThisSrvOpen->FobxList.Flink;
2903 while (ListEntry != &ThisSrvOpen->FobxList)
2904 {
2905 PFOBX Fobx;
2906
2907 Fobx = CONTAINING_RECORD(ListEntry, FOBX, FobxQLinks);
2908 ListEntry = ListEntry->Flink;
2909 RxFinalizeNetFobx(Fobx, TRUE, ForceFinalize);
2910 }
2911 }
2912
2913 /* If we have still references, don't finalize unless forced */
2914 if (ThisSrvOpen->NodeReferenceCount != 0 &&
2915 !ForceFinalize)
2916 {
2917 return FALSE;
2918 }
2919
2920 DPRINT("Finalize SRV_OPEN: %p (with %d ref), forced: %d\n", ThisSrvOpen, ThisSrvOpen->NodeReferenceCount, ForceFinalize);
2921
2922 /* Only finalize if closed, or if it wasn't already done and SRV_OPEN is in a bad shape */
2923 Fcb = (PFCB)ThisSrvOpen->pFcb;
2924 if ((!ThisSrvOpen->UpperFinalizationDone && ThisSrvOpen->Condition != Condition_Good) ||
2925 BooleanFlagOn(ThisSrvOpen->Flags, SRVOPEN_FLAG_CLOSED))
2926 {
2927 PV_NET_ROOT VNetRoot;
2928
2929 /* Associated FCB can't be fake one */
2930 ASSERT(NodeType(Fcb) != RDBSS_NTC_OPENTARGETDIR_FCB);
2931 ASSERT(RxIsFcbAcquiredExclusive (Fcb));
2932
2933 /* Purge any pending operation */
2934 RxPurgeChangeBufferingStateRequestsForSrvOpen(ThisSrvOpen);
2935
2936 /* If the FCB wasn't orphaned, inform the mini-rdr about close */
2937 if (!BooleanFlagOn(Fcb->FcbState, FCB_STATE_ORPHANED))
2938 {
2939 NTSTATUS Status;
2940
2941 MINIRDR_CALL_THROUGH(Status, Fcb->MRxDispatch, MRxForceClosed, ((PMRX_SRV_OPEN)ThisSrvOpen));
2942 (void)Status;
2943 }
2944
2945 /* Remove ourselves from the FCB */
2946 RemoveEntryList(&ThisSrvOpen->SrvOpenQLinks);
2947 InitializeListHead(&ThisSrvOpen->SrvOpenQLinks);
2948 ++Fcb->SrvOpenListVersion;
2949
2950 /* If we have a V_NET_ROOT, dereference it */
2951 VNetRoot = (PV_NET_ROOT)ThisSrvOpen->pVNetRoot;
2952 if (VNetRoot != NULL)
2953 {
2954 InterlockedDecrement((volatile long *)&VNetRoot->pNetRoot->NumberOfSrvOpens);
2955 RxDereferenceVNetRoot(VNetRoot, LHS_LockNotHeld);
2956 ThisSrvOpen->pVNetRoot = NULL;
2957 }
2958
2959 /* Finalization done */
2960 ThisSrvOpen->UpperFinalizationDone = TRUE;
2961 }
2962
2963 /* Don't free memory if still referenced */
2964 if (ThisSrvOpen->NodeReferenceCount != 0)
2965 {
2966 return FALSE;
2967 }
2968
2969 /* No key association left */
2970 ASSERT(IsListEmpty(&ThisSrvOpen->SrvOpenKeyList));
2971
2972 /* If we're still in some FCB, remove us */
2973 if (!IsListEmpty(&ThisSrvOpen->SrvOpenQLinks))
2974 {
2975 RemoveEntryList(&ThisSrvOpen->SrvOpenQLinks);
2976 }
2977
2978 /* If enclosed allocation, mark the memory zone free and dereference FCB */
2979 if (BooleanFlagOn(ThisSrvOpen->Flags, SRVOPEN_FLAG_ENCLOSED_ALLOCATED))
2980 {
2981 ClearFlag(Fcb->FcbState, FCB_STATE_SRVOPEN_USED);
2982 RxDereferenceNetFcb(Fcb);
2983 }
2984 /* Otherwise, free the memory */
2985 else
2986 {
2987 RxFreeFcbObject(ThisSrvOpen);
2988 }
2989
2990 return TRUE;
2991 }
2992
2993 /*
2994 * @implemented
2995 */
2996 BOOLEAN
2997 RxFinalizeVNetRoot(
2998 OUT PV_NET_ROOT ThisVNetRoot,
2999 IN BOOLEAN RecursiveFinalize,
3000 IN BOOLEAN ForceFinalize)
3001 {
3002 PNET_ROOT NetRoot;
3003 PRX_PREFIX_TABLE PrefixTable;
3004
3005 PAGED_CODE();
3006
3007 ASSERT(NodeType(ThisVNetRoot) == RDBSS_NTC_V_NETROOT);
3008
3009 PrefixTable = ThisVNetRoot->pNetRoot->pSrvCall->RxDeviceObject->pRxNetNameTable;
3010 ASSERT(RxIsPrefixTableLockAcquired(PrefixTable));
3011
3012 /* Only finalize if forced or if there's a single ref left */
3013 if (ThisVNetRoot->NodeReferenceCount != 1 &&
3014 !ForceFinalize)
3015 {
3016 return FALSE;
3017 }
3018
3019 DPRINT("Finalizing VNetRoot %p for %wZ\n", ThisVNetRoot, &ThisVNetRoot->PrefixEntry.Prefix);
3020
3021 NetRoot = (PNET_ROOT)ThisVNetRoot->pNetRoot;
3022 /* If it wasn't finalized yet, do it */
3023 if (!ThisVNetRoot->UpperFinalizationDone)
3024 {
3025 ASSERT(NodeType(NetRoot) == RDBSS_NTC_NETROOT);
3026
3027 /* Reference the NetRoot so that it doesn't disappear */
3028 RxReferenceNetRoot(NetRoot);
3029 RxOrphanSrvOpens(ThisVNetRoot);
3030 /* Remove us from the available VNetRoot for NetRoot */
3031 RxRemoveVirtualNetRootFromNetRoot(NetRoot, ThisVNetRoot);
3032 /* Remove extra ref */
3033 RxDereferenceNetRoot(NetRoot, LHS_ExclusiveLockHeld);
3034
3035 /* Remove ourselves from prefix table */
3036 RxRemovePrefixTableEntry(PrefixTable, &ThisVNetRoot->PrefixEntry);
3037
3038 /* Finalization done */
3039 ThisVNetRoot->UpperFinalizationDone = TRUE;
3040 }
3041
3042 /* If we're still referenced, don't go any further! */
3043 if (ThisVNetRoot->NodeReferenceCount != 1)
3044 {
3045 return FALSE;
3046 }
3047
3048 /* If there's an associated device, notify mini-rdr */
3049 if (NetRoot->pSrvCall->RxDeviceObject != NULL)
3050 {
3051 NTSTATUS Status;
3052
3053 MINIRDR_CALL_THROUGH(Status, NetRoot->pSrvCall->RxDeviceObject->Dispatch,
3054 MRxFinalizeVNetRoot, ((PMRX_V_NET_ROOT)ThisVNetRoot, FALSE));
3055 (void)Status;
3056 }
3057
3058 /* Free parameters */
3059 RxUninitializeVNetRootParameters(ThisVNetRoot->pUserName, ThisVNetRoot->pUserDomainName,
3060 ThisVNetRoot->pPassword, &ThisVNetRoot->Flags);
3061 /* Dereference our NetRoot, we won't reference it anymore */
3062 RxDereferenceNetRoot(NetRoot, LHS_ExclusiveLockHeld);
3063
3064 /* And free the object! */
3065 RxFreePoolWithTag(ThisVNetRoot, RX_V_NETROOT_POOLTAG);
3066
3067 return TRUE;
3068 }
3069
3070 NTSTATUS
3071 RxFindOrConstructVirtualNetRoot(
3072 IN PRX_CONTEXT RxContext,
3073 IN PUNICODE_STRING CanonicalName,
3074 IN NET_ROOT_TYPE NetRootType,
3075 IN PUNICODE_STRING RemainingName)
3076 {
3077 ULONG Flags;
3078 NTSTATUS Status;
3079 PVOID Container;
3080 BOOLEAN Construct;
3081 PV_NET_ROOT VNetRoot;
3082 RX_CONNECTION_ID ConnectionID;
3083 PRDBSS_DEVICE_OBJECT RxDeviceObject;
3084 LOCK_HOLDING_STATE LockHoldingState;
3085
3086 PAGED_CODE();
3087
3088 RxDeviceObject = RxContext->RxDeviceObject;
3089 ASSERT(RxDeviceObject->Dispatch != NULL);
3090 ASSERT(NodeType(RxDeviceObject->Dispatch) == RDBSS_NTC_MINIRDR_DISPATCH);
3091
3092 /* Ask the mini-rdr for connection ID */
3093 ConnectionID.SessionID = 0;
3094 if (RxDeviceObject->Dispatch->MRxGetConnectionId != NULL)
3095 {
3096 Status = RxDeviceObject->Dispatch->MRxGetConnectionId(RxContext, &ConnectionID);
3097 if (!NT_SUCCESS(Status) && Status != STATUS_NOT_IMPLEMENTED)
3098 {
3099 /* mini-rdr is expected not to fail - unless it's not implemented */
3100 DPRINT1("Failed to initialize connection ID\n");
3101 ASSERT(FALSE);
3102 }
3103 }
3104
3105 RxContext->Create.NetNamePrefixEntry = NULL;
3106
3107 Status = STATUS_MORE_PROCESSING_REQUIRED;
3108 RxAcquirePrefixTableLockShared(RxDeviceObject->pRxNetNameTable, TRUE);
3109 LockHoldingState = LHS_SharedLockHeld;
3110 Construct = TRUE;
3111 Flags = 0;
3112
3113 /* We will try twice to find a matching VNetRoot: shared locked and then exlusively locked */
3114 while (TRUE)
3115 {
3116 PNET_ROOT NetRoot;
3117 PV_NET_ROOT SavedVNetRoot;
3118
3119 /* Look in prefix table */
3120 Container = RxPrefixTableLookupName(RxDeviceObject->pRxNetNameTable, CanonicalName, RemainingName, &ConnectionID);
3121 if (Container != NULL)
3122 {
3123 /* If that's not a VNetRoot, that's a SrvCall, not interesting, loop again */
3124 if (NodeType(Container) != RDBSS_NTC_V_NETROOT)
3125 {
3126 ASSERT(NodeType(Container) == RDBSS_NTC_SRVCALL);
3127 RxDereferenceSrvCall(Container, LockHoldingState);
3128 }
3129 else
3130 {
3131 VNetRoot = Container;
3132 NetRoot = VNetRoot->NetRoot;
3133
3134 /* If the matching VNetRoot isn't in a good shape, there's something wrong - fail */
3135 if ((NetRoot->Condition != Condition_InTransition && NetRoot->Condition != Condition_Good) ||
3136 NetRoot->SrvCall->RxDeviceObject != RxContext->RxDeviceObject)
3137 {
3138 Status = STATUS_BAD_NETWORK_PATH;
3139 SavedVNetRoot = NULL;
3140 }
3141 else
3142 {
3143 LUID LogonId;
3144 ULONG SessionId;
3145 PUNICODE_STRING UserName, UserDomain, Password;
3146
3147 /* We can reuse if we use same credentials */
3148 Status = RxInitializeVNetRootParameters(RxContext, &LogonId,
3149 &SessionId, &UserName,
3150 &UserDomain, &Password,
3151 &Flags);
3152 if (NT_SUCCESS(Status))
3153 {
3154 SavedVNetRoot = VNetRoot;
3155 Status = RxCheckVNetRootCredentials(RxContext, VNetRoot,
3156 &LogonId, UserName,
3157 UserDomain, Password,
3158 Flags);
3159 if (Status == STATUS_MORE_PROCESSING_REQUIRED)
3160 {
3161 PLIST_ENTRY ListEntry;
3162
3163 for (ListEntry = NetRoot->VirtualNetRoots.Flink;
3164 ListEntry != &NetRoot->VirtualNetRoots;
3165 ListEntry = ListEntry->Flink)
3166 {
3167 SavedVNetRoot = CONTAINING_RECORD(ListEntry, V_NET_ROOT, NetRootListEntry);
3168 Status = RxCheckVNetRootCredentials(RxContext, SavedVNetRoot,
3169 &LogonId, UserName,
3170 UserDomain, Password,
3171 Flags);
3172 if (Status != STATUS_MORE_PROCESSING_REQUIRED)
3173 {
3174 break;
3175 }
3176 }
3177
3178 if (ListEntry == &NetRoot->VirtualNetRoots)
3179 {
3180 SavedVNetRoot = NULL;
3181 }
3182 }
3183
3184 if (!NT_SUCCESS(Status))
3185 {
3186 SavedVNetRoot = NULL;
3187 }
3188
3189 RxUninitializeVNetRootParameters(UserName, UserDomain, Password, &Flags);
3190 }
3191 }
3192
3193 /* We'll fail, if we had referenced a VNetRoot, dereference it */
3194 if (Status != STATUS_MORE_PROCESSING_REQUIRED && !NT_SUCCESS(Status))
3195 {
3196 if (SavedVNetRoot == NULL)
3197 {
3198 RxDereferenceVNetRoot(VNetRoot, LockHoldingState);
3199 }
3200 }
3201 /* Reference VNetRoot we'll keep, and dereference current */
3202 else if (SavedVNetRoot != VNetRoot)
3203 {
3204 RxDereferenceVNetRoot(VNetRoot, LockHoldingState);
3205 if (SavedVNetRoot != NULL)
3206 {
3207 RxReferenceVNetRoot(SavedVNetRoot);
3208 }
3209 }
3210 }
3211
3212 /* We may have found something, or we fail hard, so don't attempt to create a VNetRoot */
3213 if (Status != STATUS_MORE_PROCESSING_REQUIRED)
3214 {
3215 Construct = FALSE;
3216 break;
3217 }
3218 }
3219
3220 /* If we're locked exclusive, we won't loop again, it was the second pass */
3221 if (LockHoldingState != LHS_SharedLockHeld)
3222 {
3223 break;
3224 }
3225
3226 /* Otherwise, prepare for second pass, exclusive, making sure we can acquire without delay */
3227 if (RxAcquirePrefixTableLockExclusive(RxDeviceObject->pRxNetNameTable, FALSE))
3228 {
3229 RxReleasePrefixTableLock(RxDeviceObject->pRxNetNameTable);
3230 LockHoldingState = LHS_ExclusiveLockHeld;
3231 break;
3232 }
3233
3234 RxReleasePrefixTableLock(RxDeviceObject->pRxNetNameTable);
3235 RxAcquirePrefixTableLockExclusive(RxDeviceObject->pRxNetNameTable, TRUE);
3236 LockHoldingState = LHS_ExclusiveLockHeld;
3237 }
3238
3239 /* We didn't fail, and didn't find any VNetRoot, construct one */
3240 if (Construct)
3241 {
3242 ASSERT(LockHoldingState == LHS_ExclusiveLockHeld);
3243
3244 Status = RxConstructVirtualNetRoot(RxContext, CanonicalName, NetRootType, &VNetRoot, &LockHoldingState, &ConnectionID);
3245 ASSERT(Status != STATUS_SUCCESS || LockHoldingState != LHS_LockNotHeld);
3246
3247 if (Status == STATUS_SUCCESS)
3248 {
3249 DPRINT("CanonicalName: %wZ (%d)\n", CanonicalName, CanonicalName->Length);
3250 DPRINT("VNetRoot: %wZ (%d)\n", &VNetRoot->PrefixEntry.Prefix, VNetRoot->PrefixEntry.Prefix.Length);
3251 ASSERT(CanonicalName->Length >= VNetRoot->PrefixEntry.Prefix.Length);
3252
3253 RemainingName->Buffer = Add2Ptr(CanonicalName->Buffer, VNetRoot->PrefixEntry.Prefix.Length);
3254 RemainingName->Length = CanonicalName->Length - VNetRoot->PrefixEntry.Prefix.Length;
3255 RemainingName->MaximumLength = RemainingName->Length;
3256
3257 if (BooleanFlagOn(Flags, VNETROOT_FLAG_CSCAGENT_INSTANCE))
3258 {
3259 DPRINT("CSC instance, VNetRoot: %p\n", VNetRoot);
3260 }
3261 VNetRoot->Flags |= Flags;
3262 }
3263 }
3264
3265 /* Release the prefix table - caller expects it to be released */
3266 if (LockHoldingState != LHS_LockNotHeld)
3267 {
3268 RxReleasePrefixTableLock(RxDeviceObject->pRxNetNameTable);
3269 }
3270
3271 /* If we failed creating, quit */
3272 if (Status != STATUS_SUCCESS)
3273 {
3274 DPRINT1("RxFindOrConstructVirtualNetRoot() = Status: %x\n", Status);
3275 return Status;
3276 }
3277
3278 /* Otherwise, wait until the VNetRoot is stable */
3279 DPRINT("Waiting for stable condition for: %p\n", VNetRoot);
3280 RxWaitForStableVNetRoot(VNetRoot, RxContext);
3281 /* It's all good, update the RX_CONTEXT with all our structs */
3282 if (VNetRoot->Condition == Condition_Good)
3283 {
3284 PNET_ROOT NetRoot;
3285
3286 NetRoot = VNetRoot->NetRoot;
3287 RxContext->Create.pVNetRoot = (PMRX_V_NET_ROOT)VNetRoot;
3288 RxContext->Create.pNetRoot = (PMRX_NET_ROOT)NetRoot;
3289 RxContext->Create.pSrvCall = (PMRX_SRV_CALL)NetRoot->SrvCall;
3290 }
3291 else
3292 {
3293 RxDereferenceVNetRoot(VNetRoot, LHS_LockNotHeld);
3294 RxContext->Create.pVNetRoot = NULL;
3295 Status = STATUS_BAD_NETWORK_PATH;
3296 }
3297
3298 return Status;
3299 }
3300
3301 /*
3302 * @implemented
3303 */
3304 NTSTATUS
3305 RxFindOrCreateConnections(
3306 _In_ PRX_CONTEXT RxContext,
3307 _In_ PUNICODE_STRING CanonicalName,
3308 _In_ NET_ROOT_TYPE NetRootType,
3309 _Out_ PUNICODE_STRING LocalNetRootName,
3310 _Out_ PUNICODE_STRING FilePathName,
3311 _Inout_ PLOCK_HOLDING_STATE LockState,
3312 _In_ PRX_CONNECTION_ID RxConnectionId)
3313 {
3314 PVOID Container;
3315 PSRV_CALL SrvCall;
3316 PNET_ROOT NetRoot;
3317 PV_NET_ROOT VNetRoot;
3318 NTSTATUS Status = STATUS_UNSUCCESSFUL;
3319 PRX_PREFIX_TABLE PrefixTable;
3320 UNICODE_STRING RemainingName, NetRootName;
3321
3322 PAGED_CODE();
3323
3324 DPRINT("RxFindOrCreateConnections(%p, %wZ, %x, %p, %p, %p, %p)\n",
3325 RxContext, CanonicalName, NetRootType, LocalNetRootName,
3326 FilePathName, LockState, RxConnectionId);
3327
3328 *FilePathName = *CanonicalName;
3329 LocalNetRootName->Length = 0;
3330 LocalNetRootName->MaximumLength = 0;
3331 LocalNetRootName->Buffer = CanonicalName->Buffer;
3332
3333 /* UNC path, split it */
3334 if (FilePathName->Buffer[1] == ';')
3335 {
3336 BOOLEAN Slash;
3337 USHORT i, Length;
3338
3339 Slash = FALSE;
3340 for (i = 2; i < FilePathName->Length / sizeof(WCHAR); ++i)
3341 {
3342 if (FilePathName->Buffer[i] == OBJ_NAME_PATH_SEPARATOR)
3343 {
3344 Slash = TRUE;
3345 break;
3346 }
3347 }
3348
3349 if (!Slash)
3350 {
3351 return STATUS_OBJECT_NAME_INVALID;
3352 }
3353
3354 FilePathName->Buffer = &FilePathName->Buffer[i];
3355 Length = (USHORT)((ULONG_PTR)FilePathName->Buffer - (ULONG_PTR)LocalNetRootName->Buffer);
3356 LocalNetRootName->Length = Length;
3357 LocalNetRootName->MaximumLength = Length;
3358 FilePathName->Length -= Length;
3359
3360 DPRINT("CanonicalName: %wZ\n", CanonicalName);
3361 DPRINT(" -> FilePathName: %wZ\n", FilePathName);
3362 DPRINT(" -> LocalNetRootName: %wZ\n", LocalNetRootName);
3363 }
3364
3365 Container = NULL;
3366 PrefixTable = RxContext->RxDeviceObject->pRxNetNameTable;
3367
3368 _SEH2_TRY
3369 {
3370 RetryLookup:
3371 ASSERT(*LockState != LHS_LockNotHeld);
3372
3373 /* If previous lookup left something, dereference it */
3374 if (Container != NULL)
3375 {
3376 switch (NodeType(Container))
3377 {
3378 case RDBSS_NTC_SRVCALL:
3379 RxDereferenceSrvCall(Container, *LockState);
3380 break;
3381
3382 case RDBSS_NTC_NETROOT:
3383 RxDereferenceNetRoot(Container, *LockState);
3384 break;
3385
3386 case RDBSS_NTC_V_NETROOT:
3387 RxDereferenceVNetRoot(Container, *LockState);
3388 break;
3389
3390 default:
3391 /* Should never happen */
3392 ASSERT(FALSE);
3393 break;
3394 }
3395 }
3396
3397 /* Look for our NetRoot in prefix table */
3398 Container = RxPrefixTableLookupName(PrefixTable, FilePathName, &RemainingName, RxConnectionId);
3399 DPRINT("Container %p for path %wZ\n", Container, FilePathName);
3400
3401 while (TRUE)
3402 {
3403 UNICODE_STRING SrvCallName;
3404
3405 SrvCall = NULL;
3406 NetRoot = NULL;
3407 VNetRoot = NULL;
3408
3409 /* Assume we didn't succeed */
3410 RxContext->Create.pVNetRoot = NULL;
3411 RxContext->Create.pNetRoot = NULL;
3412 RxContext->Create.pSrvCall = NULL;
3413 RxContext->Create.Type = NetRootType;
3414
3415 /* If we found something */
3416 if (Container != NULL)
3417 {
3418 /* A VNetRoot */
3419 if (NodeType(Container) == RDBSS_NTC_V_NETROOT)
3420 {
3421 VNetRoot = Container;
3422 /* Use its NetRoot */
3423 NetRoot = VNetRoot->NetRoot;
3424
3425 /* If it's not stable, wait for it to be stable */
3426 if (NetRoot->Condition == Condition_InTransition)
3427 {
3428 RxReleasePrefixTableLock(PrefixTable);
3429 DPRINT("Waiting for stable condition for: %p\n", NetRoot);
3430 RxWaitForStableNetRoot(NetRoot, RxContext);
3431 RxAcquirePrefixTableLockExclusive(PrefixTable, TRUE);
3432 *LockState = LHS_ExclusiveLockHeld;
3433
3434 /* Now that's it's ok, retry lookup to find what we want */
3435 if (NetRoot->Condition == Condition_Good)
3436 {
3437 goto RetryLookup;
3438 }
3439 }
3440
3441 /* Is the associated netroot good? */
3442 if (NetRoot->Condition == Condition_Good)
3443 {
3444 SrvCall = (PSRV_CALL)NetRoot->pSrvCall;
3445
3446 /* If it is, and SrvCall as well, then, we have our active connection */
3447 if (SrvCall->Condition == Condition_Good &&
3448 SrvCall->RxDeviceObject == RxContext->RxDeviceObject)
3449 {
3450 RxContext->Create.pVNetRoot = (PMRX_V_NET_ROOT)VNetRoot;
3451 RxContext->Create.pNetRoot = (PMRX_NET_ROOT)NetRoot;
3452 RxContext->Create.pSrvCall = (PMRX_SRV_CALL)SrvCall;
3453
3454 Status = STATUS_CONNECTION_ACTIVE;
3455 _SEH2_LEAVE;
3456 }
3457 }
3458
3459 /* If VNetRoot was well constructed, it means the connection is active */
3460 if (VNetRoot->ConstructionStatus == STATUS_SUCCESS)
3461 {
3462 Status = STATUS_CONNECTION_ACTIVE;
3463 }
3464 else
3465 {
3466 Status = VNetRoot->ConstructionStatus;
3467 }
3468
3469 RxDereferenceVNetRoot(VNetRoot, *LockState);
3470 _SEH2_LEAVE;
3471 }
3472 /* Can only be a SrvCall */
3473 else
3474 {
3475 ASSERT(NodeType(Container) == RDBSS_NTC_SRVCALL);
3476 SrvCall = Container;
3477
3478 /* Wait for the SRV_CALL to be stable */
3479 if (SrvCall->Condition == Condition_InTransition)
3480 {
3481 RxReleasePrefixTableLock(PrefixTable);
3482 DPRINT("Waiting for stable condition for: %p\n", SrvCall);
3483 RxWaitForStableSrvCall(SrvCall, RxContext);
3484 RxAcquirePrefixTableLockExclusive(PrefixTable, TRUE);
3485 *LockState = LHS_ExclusiveLockHeld;
3486
3487 /* It went good, loop again to find what we look for */
3488 if (SrvCall->Condition == Condition_Good)
3489 {
3490 goto RetryLookup;
3491 }
3492 }
3493
3494 /* If it's not good... */
3495 if (SrvCall->Condition != Condition_Good)
3496 {
3497 /* But SRV_CALL was well constructed, assume a connection was active */
3498 if (SrvCall->Status == STATUS_SUCCESS)
3499 {
3500 Status = STATUS_CONNECTION_ACTIVE;
3501 }
3502 else
3503 {
3504 Status = SrvCall->Status;
3505 }
3506
3507 RxDereferenceSrvCall(SrvCall, *LockState);
3508 _SEH2_LEAVE;
3509 }
3510 }
3511 }
3512
3513 /* If we found a SRV_CALL not matching our DO, quit */
3514 if (SrvCall != NULL && SrvCall->Condition == Condition_Good &&
3515 SrvCall->RxDeviceObject != RxContext->RxDeviceObject)
3516 {
3517 RxDereferenceSrvCall(SrvCall, *LockState);
3518 Status = STATUS_BAD_NETWORK_NAME;
3519 _SEH2_LEAVE;
3520 }
3521
3522 /* Now, we want exclusive lock */
3523 if (*LockState == LHS_SharedLockHeld)
3524 {
3525 if (!RxAcquirePrefixTableLockExclusive(PrefixTable, FALSE))
3526 {
3527 RxReleasePrefixTableLock(PrefixTable);
3528 RxAcquirePrefixTableLockExclusive(PrefixTable, TRUE);
3529 *LockState = LHS_ExclusiveLockHeld;
3530 goto RetryLookup;
3531 }
3532
3533 RxReleasePrefixTableLock(PrefixTable);
3534 *LockState = LHS_ExclusiveLockHeld;
3535 }
3536
3537 ASSERT(*LockState == LHS_ExclusiveLockHeld);
3538
3539 /* If we reach that point, we found something, no need to create something */
3540 if (Container != NULL)
3541 {
3542 break;
3543 }
3544
3545 /* Get the name for the SRV_CALL */
3546 RxExtractServerName(FilePathName, &SrvCallName, NULL);
3547 DPRINT(" -> SrvCallName: %wZ\n", &SrvCallName);
3548 /* And create the SRV_CALL */
3549 SrvCall = RxCreateSrvCall(RxContext, &SrvCallName, NULL, RxConnectionId);
3550 if (SrvCall == NULL)
3551 {
3552 Status = STATUS_INSUFFICIENT_RESOURCES;
3553 _SEH2_LEAVE;
3554 }
3555
3556 /* Reset RX_CONTEXT, so far, connection creation isn't a success */
3557 RxReferenceSrvCall(SrvCall);
3558 RxContext->Create.pVNetRoot = NULL;
3559 RxContext->Create.pNetRoot = NULL;
3560 RxContext->Create.pSrvCall = NULL;
3561 RxContext->Create.Type = NetRootType;
3562 Container = SrvCall;
3563
3564 /* Construct SRV_CALL, ie, use mini-rdr */
3565 Status = RxConstructSrvCall(RxContext, SrvCall, LockState);
3566 ASSERT(Status != STATUS_SUCCESS || RxIsPrefixTableLockAcquired(PrefixTable));
3567 if (Status != STATUS_SUCCESS)
3568 {
3569 DPRINT1("RxConstructSrvCall() = Status: %x\n", Status);
3570 RxAcquirePrefixTableLockExclusive(PrefixTable, TRUE);
3571 RxDereferenceSrvCall(SrvCall, *LockState);
3572 RxReleasePrefixTableLock(PrefixTable);
3573 _SEH2_LEAVE;
3574 }
3575
3576 /* Loop again to make use of SRV_CALL stable condition wait */
3577 }
3578
3579 /* At that point, we have a stable SRV_CALL (either found or constructed) */
3580 ASSERT((NodeType(SrvCall) == RDBSS_NTC_SRVCALL) && (SrvCall->Condition == Condition_Good));
3581 ASSERT(NetRoot == NULL && VNetRoot == NULL);
3582 ASSERT(SrvCall->RxDeviceObject == RxContext->RxDeviceObject);
3583
3584 /* Call mini-rdr to get NetRoot name */
3585 SrvCall->RxDeviceObject->Dispatch->MRxExtractNetRootName(FilePathName, (PMRX_SRV_CALL)SrvCall, &NetRootName, NULL);
3586 /* And create the NetRoot with that name */
3587 NetRoot = RxCreateNetRoot(SrvCall, &NetRootName, 0, RxConnectionId);
3588 if (NetRoot == NULL)
3589 {
3590 Status = STATUS_INSUFFICIENT_RESOURCES;
3591 _SEH2_LEAVE;
3592 }
3593 NetRoot->Type = NetRootType;
3594
3595 RxDereferenceSrvCall(SrvCall, *LockState);
3596
3597 /* Finally, create the associated VNetRoot */
3598 VNetRoot = RxCreateVNetRoot(RxContext, NetRoot, CanonicalName, LocalNetRootName, FilePathName, RxConnectionId);
3599 if (VNetRoot == NULL)
3600 {
3601 RxFinalizeNetRoot(NetRoot, TRUE, TRUE);
3602 Status = STATUS_INSUFFICIENT_RESOURCES;
3603 _SEH2_LEAVE;
3604 }
3605 RxReferenceVNetRoot(VNetRoot);
3606
3607 /* We're get closer! */
3608 NetRoot->Condition = Condition_InTransition;
3609 RxContext->Create.pSrvCall = (PMRX_SRV_CALL)SrvCall;
3610 RxContext->Create.pNetRoot = (PMRX_NET_ROOT)NetRoot;
3611 RxContext->Create.pVNetRoot = (PMRX_V_NET_ROOT)VNetRoot;
3612
3613 /* Construct the NetRoot, involving the mini-rdr now that we have our three control structs */
3614 Status = RxConstructNetRoot(RxContext, SrvCall, NetRoot, VNetRoot, LockState);
3615 if (!NT_SUCCESS(Status))
3616 {
3617 RxTransitionVNetRoot(VNetRoot, Condition_Bad);
3618 DPRINT1("RxConstructNetRoot failed Ctxt: %p, VNet: %p, Status: %lx, Condition: %d\n", RxContext, VNetRoot, Status, VNetRoot->Condition);
3619 RxDereferenceVNetRoot(VNetRoot, *LockState);
3620
3621 RxContext->Create.pNetRoot = NULL;
3622 RxContext->Create.pVNetRoot = NULL;
3623 }
3624 else
3625 {
3626 PIO_STACK_LOCATION Stack;
3627
3628 ASSERT(*LockState == LHS_ExclusiveLockHeld);
3629
3630 Stack = RxContext->CurrentIrpSp;
3631 if (BooleanFlagOn(Stack->Parameters.Create.Options, FILE_CREATE_TREE_CONNECTION))
3632 {
3633 RxExclusivePrefixTableLockToShared(PrefixTable);
3634 *LockState = LHS_SharedLockHeld;
3635 }
3636 }
3637 }
3638 _SEH2_FINALLY
3639 {
3640 if (Status != STATUS_SUCCESS && Status != STATUS_CONNECTION_ACTIVE)
3641 {
3642 if (*LockState != LHS_LockNotHeld)
3643 {
3644 RxReleasePrefixTableLock(PrefixTable);
3645 *LockState = LHS_LockNotHeld;
3646 }
3647 }
3648 }
3649 _SEH2_END;
3650
3651 DPRINT("RxFindOrCreateConnections() = Status: %x\n", Status);
3652 return Status;
3653 }
3654
3655 /*
3656 * @implemented
3657 */
3658 VOID
3659 NTAPI
3660 RxFinishFcbInitialization(
3661 IN OUT PMRX_FCB Fcb,
3662 IN RX_FILE_TYPE FileType,
3663 IN PFCB_INIT_PACKET InitPacket OPTIONAL)
3664 {
3665 RX_FILE_TYPE OldType;
3666
3667 PAGED_CODE();
3668
3669 DPRINT("RxFinishFcbInitialization(%p, %x, %p)\n", Fcb, FileType, InitPacket);
3670
3671 OldType = NodeType(Fcb);
3672 NodeType(Fcb) = FileType;
3673 /* If mini-rdr already did the job for mailslot attributes, 0 the rest */
3674 if (BooleanFlagOn(Fcb->FcbState, FCB_STATE_TIME_AND_SIZE_ALREADY_SET) && FileType == RDBSS_NTC_MAILSLOT)
3675 {
3676 FILL_IN_FCB((PFCB)Fcb, 0, 0, 0, 0, 0, 0, 0, 0, 0);
3677 }
3678 /* Otherwise, if mini-rdr provided us with an init packet, copy its data */
3679 else if (InitPacket != NULL)
3680 {
3681 FILL_IN_FCB((PFCB)Fcb, *InitPacket->pAttributes, *InitPacket->pNumLinks,
3682 InitPacket->pCreationTime->QuadPart, InitPacket->pLastAccessTime->QuadPart,
3683 InitPacket->pLastWriteTime->QuadPart, InitPacket->pLastChangeTime->QuadPart,
3684 InitPacket->pAllocationSize->QuadPart, InitPacket->pFileSize->QuadPart,
3685 InitPacket->pValidDataLength->QuadPart);
3686 }
3687
3688 if (FileType != RDBSS_NTC_STORAGE_TYPE_UNKNOWN &&
3689 FileType != RDBSS_NTC_STORAGE_TYPE_DIRECTORY)
3690 {
3691 /* If our FCB newly points to a file, initiliaze everything related */
3692 if (FileType == RDBSS_NTC_STORAGE_TYPE_FILE)
3693
3694 {
3695 if (OldType != RDBSS_NTC_STORAGE_TYPE_FILE)
3696 {
3697 RxInitializeLowIoPerFcbInfo(&((PFCB)Fcb)->Specific.Fcb.LowIoPerFcbInfo);
3698 FsRtlInitializeFileLock(&((PFCB)Fcb)->Specific.Fcb.FileLock, RxLockOperationCompletion,
3699 RxUnlockOperation);
3700
3701 ((PFCB)Fcb)->BufferedLocks.List = NULL;
3702 ((PFCB)Fcb)->BufferedLocks.PendingLockOps = 0;
3703
3704 Fcb->Header.IsFastIoPossible = FastIoIsQuestionable;
3705 }
3706 }
3707 /* If not a file, validate type */
3708 else
3709 {
3710 ASSERT(FileType >= RDBSS_NTC_SPOOLFILE && FileType <= RDBSS_NTC_MAILSLOT);
3711 }
3712 }
3713 }
3714
3715 /*
3716 * @implemented
3717 */
3718 NTSTATUS
3719 RxFinishSrvCallConstruction(
3720 PMRX_SRVCALLDOWN_STRUCTURE Calldown)
3721 {
3722 NTSTATUS Status;
3723 PSRV_CALL SrvCall;
3724 PRX_CONTEXT Context;
3725 RX_BLOCK_CONDITION Condition;
3726 PRX_PREFIX_TABLE PrefixTable;
3727
3728 DPRINT("RxFinishSrvCallConstruction(%p)\n", Calldown);
3729
3730 SrvCall = (PSRV_CALL)Calldown->SrvCall;
3731 Context = Calldown->RxContext;
3732 PrefixTable = Context->RxDeviceObject->pRxNetNameTable;
3733
3734 /* We have a winner, notify him */
3735 if (Calldown->BestFinisher != NULL)
3736 {
3737 DPRINT("Notify the winner: %p (%wZ)\n", Calldown->BestFinisher, &Calldown->BestFinisher->DeviceName);
3738
3739 ASSERT(SrvCall->RxDeviceObject == Calldown->BestFinisher);
3740
3741 MINIRDR_CALL_THROUGH(Status, Calldown->BestFinisher->Dispatch,
3742 MRxSrvCallWinnerNotify,
3743 ((PMRX_SRV_CALL)SrvCall, TRUE,
3744 Calldown->CallbackContexts[Calldown->BestFinisherOrdinal].RecommunicateContext));
3745 if (Status != STATUS_SUCCESS)
3746 {
3747 Condition = Condition_Bad;
3748 }
3749 else
3750 {
3751 Condition = Condition_Good;
3752 }
3753 }
3754 /* Otherwise, just fail our SRV_CALL */
3755 else
3756 {
3757 Status = Calldown->CallbackContexts[0].Status;
3758 Condition = Condition_Bad;
3759 }
3760
3761 RxAcquirePrefixTableLockExclusive(PrefixTable, TRUE);
3762 RxTransitionSrvCall(SrvCall, Condition);
3763 RxFreePoolWithTag(Calldown, RX_SRVCALL_POOLTAG);
3764
3765 /* If async, finish it here, otherwise, caller has already finished the stuff */
3766 if (BooleanFlagOn(Context->Flags, RX_CONTEXT_FLAG_ASYNC_OPERATION))
3767 {
3768 DPRINT("Finishing async call\n");
3769
3770 RxReleasePrefixTableLock(PrefixTable);
3771
3772 /* Make sure we weren't cancelled in-between */
3773 if (BooleanFlagOn(Context->Flags, RX_CONTEXT_FLAG_CANCELLED))
3774 {
3775 Status = STATUS_CANCELLED;
3776 }
3777
3778 /* In case that was a create, context can be reused */
3779 if (Context->MajorFunction == IRP_MJ_CREATE)
3780 {
3781 RxpPrepareCreateContextForReuse(Context);
3782 }
3783
3784 /* If that's a failure, reset everything and return failure */
3785 if (Status != STATUS_SUCCESS)
3786 {
3787 Context->MajorFunction = Context->CurrentIrpSp->MajorFunction;
3788 if (Context->MajorFunction == IRP_MJ_DEVICE_CONTROL)
3789 {
3790 if (Context->Info.Buffer != NULL)
3791 {
3792 RxFreePool(Context->Info.Buffer);
3793 Context->Info.Buffer = NULL;
3794 }
3795 }
3796 Context->CurrentIrp->IoStatus.Information = 0;
3797 Context->CurrentIrp->IoStatus.Status = Status;
3798 RxCompleteRequest(Context, Status);
3799 }
3800 /* Otherwise, call resume routine and done! */
3801 else
3802 {
3803 Status = Context->ResumeRoutine(Context);
3804 if (Status != STATUS_PENDING)
3805 {
3806 RxCompleteRequest(Context, Status);
3807 }
3808
3809 DPRINT("Not completing, pending\n");
3810 }
3811 }
3812
3813 RxDereferenceSrvCall(SrvCall, LHS_LockNotHeld);
3814 return Status;
3815 }
3816
3817 /*
3818 * @implemented
3819 */
3820 VOID
3821 NTAPI
3822 RxFinishSrvCallConstructionDispatcher(
3823 IN PVOID Context)
3824 {
3825 KIRQL OldIrql;
3826 BOOLEAN Direct, KeepLoop;
3827
3828 DPRINT("RxFinishSrvCallConstructionDispatcher(%p)\n", Context);
3829
3830 /* In case of failure of starting dispatcher, context is not set
3831 * We keep track of it to fail associated SRV_CALL
3832 */
3833 Direct = (Context == NULL);
3834
3835 /* Separated thread, loop forever */
3836 while (TRUE)
3837 {
3838 PLIST_ENTRY ListEntry;
3839 PMRX_SRVCALLDOWN_STRUCTURE Calldown;
3840
3841 /* If there are no SRV_CALL to finalize left, just finish thread */
3842 KeAcquireSpinLock(&RxStrucSupSpinLock, &OldIrql);
3843 if (IsListEmpty(&RxSrvCalldownList))
3844 {
3845 KeepLoop = FALSE;
3846 RxSrvCallConstructionDispatcherActive = FALSE;
3847 }
3848 /* Otherwise, get the SRV_CALL to finish construction */
3849 else