d3bd22a98eb0e70a0306c4857cb14efe4b46126a
[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 NTSTATUS
2442 NTAPI
2443 RxFinalizeConnection(
2444 IN OUT PNET_ROOT NetRoot,
2445 IN OUT PV_NET_ROOT VNetRoot OPTIONAL,
2446 IN LOGICAL ForceFilesClosed)
2447 {
2448 NTSTATUS Status;
2449 PRX_PREFIX_TABLE PrefixTable;
2450 ULONG UncleanAny, UncleanDir;
2451 LONG FilesOpen, AdditionalRef;
2452 BOOLEAN PrefixLocked, FcbTableLocked, ForceClose;
2453
2454 PAGED_CODE();
2455
2456 ASSERT(NodeType(NetRoot) == RDBSS_NTC_NETROOT);
2457
2458 /* Get a BOOLEAN out of LOGICAL
2459 * -1 is like FALSE but also drops extra V_NET_ROOT reference in case of failure
2460 */
2461 ForceClose = (ForceFilesClosed == TRUE ? TRUE : FALSE);
2462
2463 /* First, delete any notification change */
2464 Status = RxCancelNotifyChangeDirectoryRequestsForVNetRoot(VNetRoot, ForceClose);
2465 /* If it failed, continue if forced */
2466 if (Status != STATUS_SUCCESS && !ForceFilesClosed)
2467 {
2468 return Status;
2469 }
2470 /* Reset status, in case notification deletion failed */
2471 Status = STATUS_SUCCESS;
2472
2473 PrefixTable = NetRoot->pSrvCall->RxDeviceObject->pRxNetNameTable;
2474
2475 PrefixLocked = FALSE;
2476 FcbTableLocked = FALSE;
2477 FilesOpen = 0;
2478 AdditionalRef = 0;
2479 UncleanAny = 0;
2480 UncleanDir = 0;
2481 _SEH2_TRY
2482 {
2483 RxAcquirePrefixTableLockExclusive(PrefixTable, TRUE);
2484 PrefixLocked = TRUE;
2485
2486 RxReferenceNetRoot(NetRoot);
2487
2488 RxAcquireFcbTableLockExclusive(&NetRoot->FcbTable, TRUE);
2489 FcbTableLocked = TRUE;
2490
2491 /* If our V_NET_ROOT wasn't finalized yet, proceed! */
2492 if (!VNetRoot->ConnectionFinalizationDone)
2493 {
2494 USHORT Bucket;
2495 PRX_FCB_TABLE FcbTable;
2496
2497 DPRINT("Finalizing connection %p: %wZ\n", NetRoot, &NetRoot->PrefixEntry.Prefix);
2498
2499 /* We'll browse all its associated FCB to check whether they're open/orphaned */
2500 FcbTable = &NetRoot->FcbTable;
2501 for (Bucket = 0; Bucket < FcbTable->NumberOfBuckets; ++Bucket)
2502 {
2503 PLIST_ENTRY BucketList, Entry;
2504
2505 BucketList = &FcbTable->HashBuckets[Bucket];
2506 Entry = BucketList->Flink;
2507 while (Entry != BucketList)
2508 {
2509 PFCB Fcb;
2510
2511 Fcb = CONTAINING_RECORD(Entry, FCB, FcbTableEntry.HashLinks);
2512 Entry = Entry->Flink;
2513
2514 /* FCB for this connection, go ahead */
2515 if (Fcb->VNetRoot == VNetRoot)
2516 {
2517 /* It's still open, and no force? Fail and keep track */
2518 if (Fcb->UncleanCount > 0 && !ForceClose)
2519 {
2520 Status = STATUS_CONNECTION_IN_USE;
2521 if (NodeType(Fcb) == RDBSS_NTC_STORAGE_TYPE_DIRECTORY)
2522 {
2523 ++UncleanDir;
2524 }
2525 else
2526 {
2527 ++UncleanAny;
2528 }
2529 }
2530 else
2531 {
2532 /* Else, force purge */
2533 ASSERT(NodeTypeIsFcb(Fcb));
2534
2535 Status = RxAcquireExclusiveFcb(NULL, Fcb);
2536 ASSERT(Status == STATUS_SUCCESS);
2537
2538 ClearFlag(Fcb->FcbState, FCB_STATE_COLLAPSING_ENABLED);
2539
2540 RxScavengeRelatedFobxs(Fcb);
2541 RxPurgeFcb(Fcb);
2542
2543 /* We don't need to release FCB lock, FCB finalize will take care of it */
2544 }
2545 }
2546 }
2547 }
2548
2549 /* No files left, our V_NET_ROOT is finalized */
2550 if (VNetRoot->NumberOfFobxs == 0)
2551 {
2552 VNetRoot->ConnectionFinalizationDone = TRUE;
2553 }
2554 }
2555
2556 /* Keep Number of open files and track of the extra reference */
2557 FilesOpen = VNetRoot->NumberOfFobxs;
2558 AdditionalRef = VNetRoot->AdditionalReferenceForDeleteFsctlTaken;
2559 /* If force close, caller doesn't want to keep connection alive
2560 * and wants it totally close, so drop the V_NET_ROOT too
2561 */
2562 if (ForceClose)
2563 {
2564 RxFinalizeVNetRoot(VNetRoot, FALSE, TRUE);
2565 }
2566 }
2567 _SEH2_FINALLY
2568 {
2569 /* Release what was acquired */
2570 if (FcbTableLocked)
2571 {
2572 RxReleaseFcbTableLock(&NetRoot->FcbTable);
2573 }
2574
2575 /* If close is forced, only fix status if there are open files */
2576 if (ForceClose)
2577 {
2578 if (Status != STATUS_SUCCESS && UncleanAny != 0)
2579 {
2580 Status = STATUS_FILES_OPEN;
2581 }
2582 }
2583 /* Else, fix status and fail closing if there are open files */
2584 else
2585 {
2586 if ((Status != STATUS_SUCCESS && UncleanAny != 0) || FilesOpen > 0)
2587 {
2588 Status = STATUS_FILES_OPEN;
2589 }
2590 }
2591
2592 DPRINT("UncleanAny: %ld, UncleanDir: %ld, FilesOpen: %ld\n", UncleanAny, UncleanDir, FilesOpen);
2593
2594 /* If we're are asked to remove the extra ref, or if closing was a success, do it;
2595 * only if it was still referenced!
2596 */
2597 if ((ForceFilesClosed == 0xFF || Status == STATUS_SUCCESS) && AdditionalRef != 0)
2598 {
2599 VNetRoot->AdditionalReferenceForDeleteFsctlTaken = 0;
2600 RxDereferenceVNetRoot(VNetRoot, LHS_ExclusiveLockHeld);
2601 }
2602
2603 if (PrefixLocked)
2604 {
2605 RxDereferenceNetRoot(NetRoot, LHS_ExclusiveLockHeld);
2606 RxReleasePrefixTableLock(PrefixTable);
2607 }
2608 }
2609 _SEH2_END;
2610
2611 return Status;
2612 }
2613
2614 /*
2615 * @implemented
2616 */
2617 VOID
2618 RxFinalizeFcbTable(
2619 IN OUT PRX_FCB_TABLE FcbTable)
2620 {
2621 USHORT Bucket;
2622
2623 PAGED_CODE();
2624
2625 /* Just delete the lock */
2626 ExDeleteResourceLite(&FcbTable->TableLock);
2627
2628 /* And make sure (checked) that the table is really empty... */
2629 for (Bucket = 0; Bucket < FcbTable->NumberOfBuckets; ++Bucket)
2630 {
2631 ASSERT(IsListEmpty(&FcbTable->HashBuckets[Bucket]));
2632 }
2633 }
2634
2635 /*
2636 * @implemented
2637 */
2638 BOOLEAN
2639 RxFinalizeNetFcb(
2640 OUT PFCB ThisFcb,
2641 IN BOOLEAN RecursiveFinalize,
2642 IN BOOLEAN ForceFinalize,
2643 IN LONG ReferenceCount)
2644 {
2645 PAGED_CODE();
2646
2647 DPRINT("RxFinalizeNetFcb(%p, %d, %d, %d)\n", ThisFcb, RecursiveFinalize, ForceFinalize, ReferenceCount);
2648 DPRINT("Finalize: %wZ\n", &ThisFcb->FcbTableEntry.Path);
2649
2650 /* Make sure we have an exclusively acquired FCB */
2651 ASSERT_CORRECT_FCB_STRUCTURE(ThisFcb);
2652 ASSERT(RxIsFcbAcquiredExclusive(ThisFcb));
2653
2654 /* We shouldn't force finalization... */
2655 ASSERT(!ForceFinalize);
2656
2657 /* If recurisve, finalize all the associated SRV_OPEN */
2658 if (RecursiveFinalize)
2659 {
2660 PLIST_ENTRY ListEntry;
2661
2662 for (ListEntry = ThisFcb->SrvOpenList.Flink;
2663 ListEntry != &ThisFcb->SrvOpenList;
2664 ListEntry = ListEntry->Flink)
2665 {
2666 PSRV_OPEN SrvOpen;
2667
2668 SrvOpen = CONTAINING_RECORD(ListEntry, SRV_OPEN, SrvOpenQLinks);
2669 RxFinalizeSrvOpen(SrvOpen, TRUE, ForceFinalize);
2670 }
2671 }
2672 /* If FCB is still in use, that's over */
2673 else
2674 {
2675 if (ThisFcb->OpenCount != 0 || ThisFcb->UncleanCount != 0)
2676 {
2677 ASSERT(ReferenceCount > 0);
2678
2679 return FALSE;
2680 }
2681 }
2682
2683 ASSERT(ReferenceCount >= 1);
2684
2685 /* If FCB is still referenced, that's over - unless you force it and want to BSOD somewhere */
2686 if (ReferenceCount != 1 && !ForceFinalize)
2687 {
2688 return FALSE;
2689 }
2690
2691 ASSERT(ForceFinalize || ((ThisFcb->OpenCount == 0) && (ThisFcb->UncleanCount == 0)));
2692
2693 DPRINT("Finalizing FCB open: %d (%d)\n", ThisFcb->OpenCount, ForceFinalize);
2694
2695 /* If finalization was not already initiated, go ahead */
2696 if (!ThisFcb->UpperFinalizationDone)
2697 {
2698 /* Free any FCB_LOCK */
2699 if (NodeType(ThisFcb) == RDBSS_NTC_STORAGE_TYPE_FILE)
2700 {
2701 FsRtlUninitializeFileLock(&ThisFcb->Specific.Fcb.FileLock);
2702
2703 while (ThisFcb->BufferedLocks.List != NULL)
2704 {
2705 PFCB_LOCK Entry;
2706
2707 Entry = ThisFcb->BufferedLocks.List;
2708 ThisFcb->BufferedLocks.List = Entry->Next;
2709
2710 RxFreePool(Entry);
2711 }
2712 }
2713
2714 /* If not orphaned, it still has a NET_ROOT and potentially is still in a table */
2715 if (!BooleanFlagOn(ThisFcb->FcbState, FCB_STATE_ORPHANED))
2716 {
2717 PNET_ROOT NetRoot;
2718
2719 NetRoot = (PNET_ROOT)ThisFcb->pNetRoot;
2720
2721 ASSERT(RxIsFcbTableLockExclusive(&NetRoot->FcbTable));
2722 /* So, remove it */
2723 if (!BooleanFlagOn(ThisFcb->FcbState, FCB_STATE_NAME_ALREADY_REMOVED))
2724 {
2725 RxFcbTableRemoveFcb(&NetRoot->FcbTable, ThisFcb);
2726 }
2727 }
2728
2729 ThisFcb->UpperFinalizationDone = TRUE;
2730 }
2731
2732 ASSERT(ReferenceCount >= 1);
2733
2734 /* Even if forced, don't allow broken free */
2735 if (ReferenceCount != 1)
2736 {
2737 return FALSE;
2738 }
2739
2740 /* Now, release everything */
2741 if (ThisFcb->pBufferingStateChangeCompletedEvent != NULL)
2742 {
2743 RxFreePool(ThisFcb->pBufferingStateChangeCompletedEvent);
2744 }
2745
2746 if (ThisFcb->MRxDispatch != NULL)
2747 {
2748 ThisFcb->MRxDispatch->MRxDeallocateForFcb(RX_GET_MRX_FCB(ThisFcb));
2749 }
2750
2751 ExDeleteResourceLite(ThisFcb->BufferedLocks.Resource);
2752 ExDeleteResourceLite(ThisFcb->Header.Resource);
2753 ExDeleteResourceLite(ThisFcb->Header.PagingIoResource);
2754
2755 InterlockedDecrement((volatile long *)&ThisFcb->pNetRoot->NumberOfFcbs);
2756 RxDereferenceVNetRoot(ThisFcb->VNetRoot, LHS_LockNotHeld);
2757
2758 ASSERT(IsListEmpty(&ThisFcb->FcbTableEntry.HashLinks));
2759 ASSERT(!ThisFcb->fMiniInited);
2760
2761 /* And free the object */
2762 RxFreeFcbObject(ThisFcb);
2763
2764 return TRUE;
2765 }
2766
2767 /*
2768 * @implemented
2769 */
2770 BOOLEAN
2771 RxFinalizeNetFobx(
2772 _Out_ PFOBX ThisFobx,
2773 _In_ BOOLEAN RecursiveFinalize,
2774 _In_ BOOLEAN ForceFinalize)
2775 {
2776 PFCB Fcb;
2777 PSRV_OPEN SrvOpen;
2778
2779 PAGED_CODE();
2780
2781 ASSERT(NodeType(ThisFobx) == RDBSS_NTC_FOBX);
2782
2783 /* Only finalize if forced or if there's no ref left */
2784 if (ThisFobx->NodeReferenceCount != 0 &&
2785 !ForceFinalize)
2786 {
2787 return FALSE;
2788 }
2789
2790 DPRINT("Finalize Fobx: %p (with %d ref), forced: %d\n", ThisFobx, ThisFobx->NodeReferenceCount, ForceFinalize);
2791
2792 SrvOpen = ThisFobx->SrvOpen;
2793 Fcb = SrvOpen->Fcb;
2794 /* If it wasn't finalized yet, do it */
2795 if (!ThisFobx->UpperFinalizationDone)
2796 {
2797 ASSERT(NodeType(SrvOpen->Fcb) != RDBSS_NTC_OPENTARGETDIR_FCB);
2798 ASSERT(RxIsFcbAcquiredExclusive(SrvOpen->Fcb));
2799
2800 /* Remove it from the SRV_OPEN */
2801 RemoveEntryList(&ThisFobx->FobxQLinks);
2802
2803 /* If we were used to browse a directory, free the query buffer */
2804 if (BooleanFlagOn(ThisFobx->Flags, FOBX_FLAG_FREE_UNICODE))
2805 {
2806 RxFreePoolWithTag(ThisFobx->UnicodeQueryTemplate.Buffer, RX_DIRCTL_POOLTAG);
2807 }
2808
2809 /* Notify the mini-rdr */
2810 if (Fcb->MRxDispatch != NULL && Fcb->MRxDispatch->MRxDeallocateForFobx != NULL)
2811 {
2812 Fcb->MRxDispatch->MRxDeallocateForFobx((PMRX_FOBX)ThisFobx);
2813 }
2814
2815 /* If the SRV_OPEN wasn't closed yet, do it */
2816 if (!BooleanFlagOn(ThisFobx->Flags, FOBX_FLAG_SRVOPEN_CLOSED))
2817 {
2818 NTSTATUS Status;
2819
2820 Status = RxCloseAssociatedSrvOpen(ThisFobx, FALSE);
2821 DPRINT("Closing SRV_OPEN %p for %p: %x\n", SrvOpen, ThisFobx, Status);
2822 }
2823
2824 /* Finalization done */
2825 ThisFobx->UpperFinalizationDone = TRUE;
2826 }
2827
2828 /* If we're still referenced, don't go any further! */
2829 if (ThisFobx->NodeReferenceCount != 0)
2830 {
2831 return FALSE;
2832 }
2833
2834 /* At that point, everything should be closed */
2835 ASSERT(IsListEmpty(&ThisFobx->ClosePendingList));
2836
2837 /* Was the FOBX allocated with another object?
2838 * If so, mark the buffer free in said object
2839 */
2840 if (ThisFobx == Fcb->InternalFobx)
2841 {
2842 ClearFlag(Fcb->FcbState, FCB_STATE_FOBX_USED);
2843 }
2844 else if (ThisFobx == SrvOpen->InternalFobx)
2845 {
2846 ClearFlag(SrvOpen->Flags, SRVOPEN_FLAG_FOBX_USED);
2847 }
2848
2849 ThisFobx->pSrvOpen = NULL;
2850
2851 /* A FOBX less */
2852 InterlockedDecrement((volatile long *)&SrvOpen->pVNetRoot->NumberOfFobxs);
2853
2854 RxDereferenceSrvOpen(SrvOpen, LHS_ExclusiveLockHeld);
2855
2856 /* If it wasn't allocated with another object, free the FOBX */
2857 if (!BooleanFlagOn(ThisFobx->Flags, FOBX_FLAG_ENCLOSED_ALLOCATED))
2858 {
2859 RxFreeFcbObject(ThisFobx);
2860 }
2861
2862 return TRUE;
2863 }
2864
2865 /*
2866 * @implemented
2867 */
2868 BOOLEAN
2869 RxFinalizeNetRoot(
2870 OUT PNET_ROOT ThisNetRoot,
2871 IN BOOLEAN RecursiveFinalize,
2872 IN BOOLEAN ForceFinalize)
2873 {
2874 PSRV_CALL SrvCall;
2875 PRX_FCB_TABLE FcbTable;
2876 PRX_PREFIX_TABLE PrefixTable;
2877
2878 PAGED_CODE();
2879
2880 ASSERT(NodeType(ThisNetRoot) == RDBSS_NTC_NETROOT);
2881
2882 PrefixTable = ThisNetRoot->pSrvCall->RxDeviceObject->pRxNetNameTable;
2883 ASSERT(RxIsPrefixTableLockAcquired(PrefixTable));
2884
2885 /* If sme finalization is already ongoing, leave */
2886 if (BooleanFlagOn(ThisNetRoot->Flags, NETROOT_FLAG_FINALIZATION_IN_PROGRESS))
2887 {
2888 return FALSE;
2889 }
2890
2891 /* Mark we're finalizing */
2892 SetFlag(ThisNetRoot->Flags, NETROOT_FLAG_FINALIZATION_IN_PROGRESS);
2893
2894 FcbTable = &ThisNetRoot->FcbTable;
2895 /* Did caller asked us to finalize any associated FCB? */
2896 if (RecursiveFinalize)
2897 {
2898 USHORT Bucket;
2899
2900 /* Browse all the FCBs in our FCB table */
2901 RxAcquireFcbTableLockExclusive(FcbTable, TRUE);
2902 for (Bucket = 0; Bucket < FcbTable->NumberOfBuckets; ++Bucket)
2903 {
2904 PLIST_ENTRY HashBucket, ListEntry;
2905
2906 HashBucket = &FcbTable->HashBuckets[Bucket];
2907 ListEntry = HashBucket->Flink;
2908 while (ListEntry != HashBucket)
2909 {
2910 PFCB Fcb;
2911
2912 Fcb = CONTAINING_RECORD(ListEntry, FCB, FcbTableEntry.HashLinks);
2913 ASSERT(NodeTypeIsFcb(Fcb));
2914
2915 ListEntry = ListEntry->Flink;
2916
2917 /* If the FCB isn't orphaned, then, it's time to purge it */
2918 if (!BooleanFlagOn(Fcb->FcbState, FCB_STATE_ORPHANED))
2919 {
2920 NTSTATUS Status;
2921
2922 Status = RxAcquireExclusiveFcb(NULL, Fcb);
2923 ASSERT(Status == STATUS_SUCCESS);
2924 RxPurgeFcb(Fcb);
2925 }
2926 }
2927 }
2928 RxReleaseFcbTableLock(FcbTable);
2929 }
2930
2931 /* Only finalize if forced or if there's a single ref left */
2932 if (ThisNetRoot->NodeReferenceCount != 1 && !ForceFinalize)
2933 {
2934 return FALSE;
2935 }
2936
2937 DPRINT("Finalizing NetRoot %p for %wZ\n", ThisNetRoot, &ThisNetRoot->PrefixEntry.Prefix);
2938
2939 /* If we're still referenced, don't go any further! */
2940 if (ThisNetRoot->NodeReferenceCount != 1)
2941 {
2942 return FALSE;
2943 }
2944
2945 /* Finalize the FCB table (and make sure it's empty!) */
2946 RxFinalizeFcbTable(FcbTable);
2947
2948 /* If name wasn't remove already, do it now */
2949 if (!BooleanFlagOn(ThisNetRoot->Flags, NETROOT_FLAG_NAME_ALREADY_REMOVED))
2950 {
2951 RxRemovePrefixTableEntry(PrefixTable, &ThisNetRoot->PrefixEntry);
2952 }
2953
2954 /* Delete the object */
2955 SrvCall = (PSRV_CALL)ThisNetRoot->pSrvCall;
2956 RxFreeObject(ThisNetRoot);
2957
2958 /* And dereference the associated SRV_CALL */
2959 if (SrvCall != NULL)
2960 {
2961 RxDereferenceSrvCall(SrvCall, LHS_ExclusiveLockHeld);
2962 }
2963
2964 return TRUE;
2965 }
2966
2967 /*
2968 * @implemented
2969 */
2970 BOOLEAN
2971 RxFinalizeSrvCall(
2972 OUT PSRV_CALL ThisSrvCall,
2973 IN BOOLEAN RecursiveFinalize,
2974 IN BOOLEAN ForceFinalize)
2975 {
2976 PRX_PREFIX_TABLE PrefixTable;
2977
2978 PAGED_CODE();
2979
2980 ASSERT(NodeType(ThisSrvCall) == RDBSS_NTC_SRVCALL);
2981
2982 PrefixTable = ThisSrvCall->RxDeviceObject->pRxNetNameTable;
2983 ASSERT(RxIsPrefixTableLockAcquired(PrefixTable));
2984
2985 /* Only finalize if forced or if there's a single ref left */
2986 if (ThisSrvCall->NodeReferenceCount != 1 &&
2987 !ForceFinalize)
2988 {
2989 return FALSE;
2990 }
2991
2992 DPRINT("Finalizing SrvCall %p for %wZ\n", ThisSrvCall, &ThisSrvCall->PrefixEntry.Prefix);
2993
2994 /* If it wasn't finalized yet, do it */
2995 if (!ThisSrvCall->UpperFinalizationDone)
2996 {
2997 BOOLEAN WillFree;
2998
2999 /* Remove ourselves from prefix table */
3000 RxRemovePrefixTableEntry(PrefixTable, &ThisSrvCall->PrefixEntry);
3001
3002 /* Remember our third arg, in case we get queued for later execution */
3003 if (ForceFinalize)
3004 {
3005 SetFlag(ThisSrvCall->Flags, SRVCALL_FLAG_FORCE_FINALIZED);
3006 }
3007
3008 /* And done */
3009 ThisSrvCall->UpperFinalizationDone = TRUE;
3010
3011 /* Would defered execution free the object? */
3012 WillFree = (ThisSrvCall->NodeReferenceCount == 1);
3013
3014 /* If we have a device object */
3015 if (ThisSrvCall->RxDeviceObject != NULL)
3016 {
3017 NTSTATUS Status;
3018
3019 /* If we're not executing in the RDBSS thread, queue for execution within the thread */
3020 if (RxGetRDBSSProcess() != IoGetCurrentProcess())
3021 {
3022 /* Extra ref, as usual */
3023 InterlockedIncrement((volatile long *)&ThisSrvCall->NodeReferenceCount);
3024 /* And dispatch */
3025 RxDispatchToWorkerThread(ThisSrvCall->RxDeviceObject, DelayedWorkQueue, RxpDestroySrvCall, ThisSrvCall);
3026
3027 /* Return to the caller, in advance, whether we're freeing the object or not */
3028 return WillFree;
3029 }
3030
3031 /* If in the right thread already, call the mini-rdr */
3032 MINIRDR_CALL_THROUGH(Status, ThisSrvCall->RxDeviceObject->Dispatch,
3033 MRxFinalizeSrvCall, ((PMRX_SRV_CALL)ThisSrvCall, ForceFinalize));
3034 (void)Status;
3035 }
3036 }
3037
3038 /* If we're still referenced, don't go any further! */
3039 if (ThisSrvCall->NodeReferenceCount != 1)
3040 {
3041 return FALSE;
3042 }
3043
3044 /* Don't leak */
3045 if (ThisSrvCall->pDomainName != NULL)
3046 {
3047 RxFreePool(ThisSrvCall->pDomainName);
3048 }
3049
3050 /* And free! */
3051 RxTearDownBufferingManager(ThisSrvCall);
3052 RxFreeObject(ThisSrvCall);
3053
3054 return TRUE;
3055 }
3056
3057 /*
3058 * @implemented
3059 */
3060 BOOLEAN
3061 RxFinalizeSrvOpen(
3062 OUT PSRV_OPEN ThisSrvOpen,
3063 IN BOOLEAN RecursiveFinalize,
3064 IN BOOLEAN ForceFinalize)
3065 {
3066 PFCB Fcb;
3067
3068 PAGED_CODE();
3069
3070 /* We have to have a SRV_OPEN */
3071 ASSERT(NodeType(ThisSrvOpen) == RDBSS_NTC_SRVOPEN);
3072
3073 /* If that's a recursive finalization, finalize any related FOBX */
3074 if (RecursiveFinalize)
3075 {
3076 PLIST_ENTRY ListEntry;
3077
3078 ListEntry = ThisSrvOpen->FobxList.Flink;
3079 while (ListEntry != &ThisSrvOpen->FobxList)
3080 {
3081 PFOBX Fobx;
3082
3083 Fobx = CONTAINING_RECORD(ListEntry, FOBX, FobxQLinks);
3084 ListEntry = ListEntry->Flink;
3085 RxFinalizeNetFobx(Fobx, TRUE, ForceFinalize);
3086 }
3087 }
3088
3089 /* If we have still references, don't finalize unless forced */
3090 if (ThisSrvOpen->NodeReferenceCount != 0 &&
3091 !ForceFinalize)
3092 {
3093 return FALSE;
3094 }
3095
3096 DPRINT("Finalize SRV_OPEN: %p (with %d ref), forced: %d\n", ThisSrvOpen, ThisSrvOpen->NodeReferenceCount, ForceFinalize);
3097
3098 /* Only finalize if closed, or if it wasn't already done and SRV_OPEN is in a bad shape */
3099 Fcb = (PFCB)ThisSrvOpen->pFcb;
3100 if ((!ThisSrvOpen->UpperFinalizationDone && ThisSrvOpen->Condition != Condition_Good) ||
3101 BooleanFlagOn(ThisSrvOpen->Flags, SRVOPEN_FLAG_CLOSED))
3102 {
3103 PV_NET_ROOT VNetRoot;
3104
3105 /* Associated FCB can't be fake one */
3106 ASSERT(NodeType(Fcb) != RDBSS_NTC_OPENTARGETDIR_FCB);
3107 ASSERT(RxIsFcbAcquiredExclusive (Fcb));
3108
3109 /* Purge any pending operation */
3110 RxPurgeChangeBufferingStateRequestsForSrvOpen(ThisSrvOpen);
3111
3112 /* If the FCB wasn't orphaned, inform the mini-rdr about close */
3113 if (!BooleanFlagOn(Fcb->FcbState, FCB_STATE_ORPHANED))
3114 {
3115 NTSTATUS Status;
3116
3117 MINIRDR_CALL_THROUGH(Status, Fcb->MRxDispatch, MRxForceClosed, ((PMRX_SRV_OPEN)ThisSrvOpen));
3118 (void)Status;
3119 }
3120
3121 /* Remove ourselves from the FCB */
3122 RemoveEntryList(&ThisSrvOpen->SrvOpenQLinks);
3123 InitializeListHead(&ThisSrvOpen->SrvOpenQLinks);
3124 ++Fcb->SrvOpenListVersion;
3125
3126 /* If we have a V_NET_ROOT, dereference it */
3127 VNetRoot = (PV_NET_ROOT)ThisSrvOpen->pVNetRoot;
3128 if (VNetRoot != NULL)
3129 {
3130 InterlockedDecrement((volatile long *)&VNetRoot->pNetRoot->NumberOfSrvOpens);
3131 RxDereferenceVNetRoot(VNetRoot, LHS_LockNotHeld);
3132 ThisSrvOpen->pVNetRoot = NULL;
3133 }
3134
3135 /* Finalization done */
3136 ThisSrvOpen->UpperFinalizationDone = TRUE;
3137 }
3138
3139 /* Don't free memory if still referenced */
3140 if (ThisSrvOpen->NodeReferenceCount != 0)
3141 {
3142 return FALSE;
3143 }
3144
3145 /* No key association left */
3146 ASSERT(IsListEmpty(&ThisSrvOpen->SrvOpenKeyList));
3147
3148 /* If we're still in some FCB, remove us */
3149 if (!IsListEmpty(&ThisSrvOpen->SrvOpenQLinks))
3150 {
3151 RemoveEntryList(&ThisSrvOpen->SrvOpenQLinks);
3152 }
3153
3154 /* If enclosed allocation, mark the memory zone free and dereference FCB */
3155 if (BooleanFlagOn(ThisSrvOpen->Flags, SRVOPEN_FLAG_ENCLOSED_ALLOCATED))
3156 {
3157 ClearFlag(Fcb->FcbState, FCB_STATE_SRVOPEN_USED);
3158 RxDereferenceNetFcb(Fcb);
3159 }
3160 /* Otherwise, free the memory */
3161 else
3162 {
3163 RxFreeFcbObject(ThisSrvOpen);
3164 }
3165
3166 return TRUE;
3167 }
3168
3169 /*
3170 * @implemented
3171 */
3172 BOOLEAN
3173 RxFinalizeVNetRoot(
3174 OUT PV_NET_ROOT ThisVNetRoot,
3175 IN BOOLEAN RecursiveFinalize,
3176 IN BOOLEAN ForceFinalize)
3177 {
3178 PNET_ROOT NetRoot;
3179 PRX_PREFIX_TABLE PrefixTable;
3180
3181 PAGED_CODE();
3182
3183 ASSERT(NodeType(ThisVNetRoot) == RDBSS_NTC_V_NETROOT);
3184
3185 PrefixTable = ThisVNetRoot->pNetRoot->pSrvCall->RxDeviceObject->pRxNetNameTable;
3186 ASSERT(RxIsPrefixTableLockAcquired(PrefixTable));
3187
3188 /* Only finalize if forced or if there's a single ref left */
3189 if (ThisVNetRoot->NodeReferenceCount != 1 &&
3190 !ForceFinalize)
3191 {
3192 return FALSE;
3193 }
3194
3195 DPRINT("Finalizing VNetRoot %p for %wZ\n", ThisVNetRoot, &ThisVNetRoot->PrefixEntry.Prefix);
3196
3197 NetRoot = (PNET_ROOT)ThisVNetRoot->pNetRoot;
3198 /* If it wasn't finalized yet, do it */
3199 if (!ThisVNetRoot->UpperFinalizationDone)
3200 {
3201 ASSERT(NodeType(NetRoot) == RDBSS_NTC_NETROOT);
3202
3203 /* Reference the NetRoot so that it doesn't disappear */
3204 RxReferenceNetRoot(NetRoot);
3205 RxOrphanSrvOpens(ThisVNetRoot);
3206 /* Remove us from the available VNetRoot for NetRoot */
3207 RxRemoveVirtualNetRootFromNetRoot(NetRoot, ThisVNetRoot);
3208 /* Remove extra ref */
3209 RxDereferenceNetRoot(NetRoot, LHS_ExclusiveLockHeld);
3210
3211 /* Remove ourselves from prefix table */
3212 RxRemovePrefixTableEntry(PrefixTable, &ThisVNetRoot->PrefixEntry);
3213
3214 /* Finalization done */
3215 ThisVNetRoot->UpperFinalizationDone = TRUE;
3216 }
3217
3218 /* If we're still referenced, don't go any further! */
3219 if (ThisVNetRoot->NodeReferenceCount != 1)
3220 {
3221 return FALSE;
3222 }
3223
3224 /* If there's an associated device, notify mini-rdr */
3225 if (NetRoot->pSrvCall->RxDeviceObject != NULL)
3226 {
3227 NTSTATUS Status;
3228
3229 MINIRDR_CALL_THROUGH(Status, NetRoot->pSrvCall->RxDeviceObject->Dispatch,
3230 MRxFinalizeVNetRoot, ((PMRX_V_NET_ROOT)ThisVNetRoot, FALSE));
3231 (void)Status;
3232 }
3233
3234 /* Free parameters */
3235 RxUninitializeVNetRootParameters(ThisVNetRoot->pUserName, ThisVNetRoot->pUserDomainName,
3236 ThisVNetRoot->pPassword, &ThisVNetRoot->Flags);
3237 /* Dereference our NetRoot, we won't reference it anymore */
3238 RxDereferenceNetRoot(NetRoot, LHS_ExclusiveLockHeld);
3239
3240 /* And free the object! */
3241 RxFreePoolWithTag(ThisVNetRoot, RX_V_NETROOT_POOLTAG);
3242
3243 return TRUE;
3244 }
3245
3246 NTSTATUS
3247 RxFindOrConstructVirtualNetRoot(
3248 IN PRX_CONTEXT RxContext,
3249 IN PUNICODE_STRING CanonicalName,
3250 IN NET_ROOT_TYPE NetRootType,
3251 IN PUNICODE_STRING RemainingName)
3252 {
3253 ULONG Flags;
3254 NTSTATUS Status;
3255 PVOID Container;
3256 BOOLEAN Construct;
3257 PV_NET_ROOT VNetRoot;
3258 RX_CONNECTION_ID ConnectionID;
3259 PRDBSS_DEVICE_OBJECT RxDeviceObject;
3260 LOCK_HOLDING_STATE LockHoldingState;
3261
3262 PAGED_CODE();
3263
3264 RxDeviceObject = RxContext->RxDeviceObject;
3265 ASSERT(RxDeviceObject->Dispatch != NULL);
3266 ASSERT(NodeType(RxDeviceObject->Dispatch) == RDBSS_NTC_MINIRDR_DISPATCH);
3267
3268 /* Ask the mini-rdr for connection ID */
3269 ConnectionID.SessionID = 0;
3270 if (RxDeviceObject->Dispatch->MRxGetConnectionId != NULL)
3271 {
3272 Status = RxDeviceObject->Dispatch->MRxGetConnectionId(RxContext, &ConnectionID);
3273 if (!NT_SUCCESS(Status) && Status != STATUS_NOT_IMPLEMENTED)
3274 {
3275 /* mini-rdr is expected not to fail - unless it's not implemented */
3276 DPRINT1("Failed to initialize connection ID\n");
3277 ASSERT(FALSE);
3278 }
3279 }
3280
3281 RxContext->Create.NetNamePrefixEntry = NULL;
3282
3283 Status = STATUS_MORE_PROCESSING_REQUIRED;
3284 RxAcquirePrefixTableLockShared(RxDeviceObject->pRxNetNameTable, TRUE);
3285 LockHoldingState = LHS_SharedLockHeld;
3286 Construct = TRUE;
3287 Flags = 0;
3288
3289 /* We will try twice to find a matching VNetRoot: shared locked and then exlusively locked */
3290 while (TRUE)
3291 {
3292 PNET_ROOT NetRoot;
3293 PV_NET_ROOT SavedVNetRoot;
3294
3295 /* Look in prefix table */
3296 Container = RxPrefixTableLookupName(RxDeviceObject->pRxNetNameTable, CanonicalName, RemainingName, &ConnectionID);
3297 if (Container != NULL)
3298 {
3299 /* If that's not a VNetRoot, that's a SrvCall, not interesting, loop again */
3300 if (NodeType(Container) != RDBSS_NTC_V_NETROOT)
3301 {
3302 ASSERT(NodeType(Container) == RDBSS_NTC_SRVCALL);
3303 RxDereferenceSrvCall(Container, LockHoldingState);
3304 }
3305 else
3306 {
3307 VNetRoot = Container;
3308 NetRoot = VNetRoot->NetRoot;
3309
3310 /* If the matching VNetRoot isn't in a good shape, there's something wrong - fail */
3311 if ((NetRoot->Condition != Condition_InTransition && NetRoot->Condition != Condition_Good) ||
3312 NetRoot->SrvCall->RxDeviceObject != RxContext->RxDeviceObject)
3313 {
3314 Status = STATUS_BAD_NETWORK_PATH;
3315 SavedVNetRoot = NULL;
3316 }
3317 else
3318 {
3319 LUID LogonId;
3320 ULONG SessionId;
3321 PUNICODE_STRING UserName, UserDomain, Password;
3322
3323 /* We can reuse if we use same credentials */
3324 Status = RxInitializeVNetRootParameters(RxContext, &LogonId,
3325 &SessionId, &UserName,
3326 &UserDomain, &Password,
3327 &Flags);
3328 if (NT_SUCCESS(Status))
3329 {
3330 SavedVNetRoot = VNetRoot;
3331 Status = RxCheckVNetRootCredentials(RxContext, VNetRoot,
3332 &LogonId, UserName,
3333 UserDomain, Password,
3334 Flags);
3335 if (Status == STATUS_MORE_PROCESSING_REQUIRED)
3336 {
3337 PLIST_ENTRY ListEntry;
3338
3339 for (ListEntry = NetRoot->VirtualNetRoots.Flink;
3340 ListEntry != &NetRoot->VirtualNetRoots;
3341 ListEntry = ListEntry->Flink)
3342 {
3343 SavedVNetRoot = CONTAINING_RECORD(ListEntry, V_NET_ROOT, NetRootListEntry);
3344 Status = RxCheckVNetRootCredentials(RxContext, SavedVNetRoot,
3345 &LogonId, UserName,
3346 UserDomain, Password,
3347 Flags);
3348 if (Status != STATUS_MORE_PROCESSING_REQUIRED)
3349 {
3350 break;
3351 }
3352 }
3353
3354 if (ListEntry == &NetRoot->VirtualNetRoots)
3355 {
3356 SavedVNetRoot = NULL;
3357 }
3358 }
3359
3360 if (!NT_SUCCESS(Status))
3361 {
3362 SavedVNetRoot = NULL;
3363 }
3364
3365 RxUninitializeVNetRootParameters(UserName, UserDomain, Password, &Flags);
3366 }
3367 }
3368
3369 /* We'll fail, if we had referenced a VNetRoot, dereference it */
3370 if (Status != STATUS_MORE_PROCESSING_REQUIRED && !NT_SUCCESS(Status))
3371 {
3372 if (SavedVNetRoot == NULL)
3373 {
3374 RxDereferenceVNetRoot(VNetRoot, LockHoldingState);
3375 }
3376 }
3377 /* Reference VNetRoot we'll keep, and dereference current */
3378 else if (SavedVNetRoot != VNetRoot)
3379 {
3380 RxDereferenceVNetRoot(VNetRoot, LockHoldingState);
3381 if (SavedVNetRoot != NULL)
3382 {
3383 RxReferenceVNetRoot(SavedVNetRoot);
3384 }
3385 }
3386 }
3387
3388 /* We may have found something, or we fail hard, so don't attempt to create a VNetRoot */
3389 if (Status != STATUS_MORE_PROCESSING_REQUIRED)
3390 {
3391 Construct = FALSE;
3392 break;
3393 }
3394 }
3395
3396 /* If we're locked exclusive, we won't loop again, it was the second pass */
3397 if (LockHoldingState != LHS_SharedLockHeld)
3398 {
3399 break;
3400 }
3401
3402 /* Otherwise, prepare for second pass, exclusive, making sure we can acquire without delay */
3403 if (RxAcquirePrefixTableLockExclusive(RxDeviceObject->pRxNetNameTable, FALSE))
3404 {
3405 RxReleasePrefixTableLock(RxDeviceObject->pRxNetNameTable);
3406 LockHoldingState = LHS_ExclusiveLockHeld;
3407 break;
3408 }
3409
3410 RxReleasePrefixTableLock(RxDeviceObject->pRxNetNameTable);
3411 RxAcquirePrefixTableLockExclusive(RxDeviceObject->pRxNetNameTable, TRUE);
3412 LockHoldingState = LHS_ExclusiveLockHeld;
3413 }
3414
3415 /* We didn't fail, and didn't find any VNetRoot, construct one */
3416 if (Construct)
3417 {
3418 ASSERT(LockHoldingState == LHS_ExclusiveLockHeld);
3419
3420 Status = RxConstructVirtualNetRoot(RxContext, CanonicalName, NetRootType, &VNetRoot, &LockHoldingState, &ConnectionID);
3421 ASSERT(Status != STATUS_SUCCESS || LockHoldingState != LHS_LockNotHeld);
3422
3423 if (Status == STATUS_SUCCESS)
3424 {
3425 DPRINT("CanonicalName: %wZ (%d)\n", CanonicalName, CanonicalName->Length);
3426 DPRINT("VNetRoot: %wZ (%d)\n", &VNetRoot->PrefixEntry.Prefix, VNetRoot->PrefixEntry.Prefix.Length);
3427 ASSERT(CanonicalName->Length >= VNetRoot->PrefixEntry.Prefix.Length);
3428
3429 RemainingName->Buffer = Add2Ptr(CanonicalName->Buffer, VNetRoot->PrefixEntry.Prefix.Length);
3430 RemainingName->Length = CanonicalName->Length - VNetRoot->PrefixEntry.Prefix.Length;
3431 RemainingName->MaximumLength = RemainingName->Length;
3432
3433 if (BooleanFlagOn(Flags, VNETROOT_FLAG_CSCAGENT_INSTANCE))
3434 {
3435 DPRINT("CSC instance, VNetRoot: %p\n", VNetRoot);
3436 }
3437 VNetRoot->Flags |= Flags;
3438 }
3439 }
3440
3441 /* Release the prefix table - caller expects it to be released */
3442 if (LockHoldingState != LHS_LockNotHeld)
3443 {
3444 RxReleasePrefixTableLock(RxDeviceObject->pRxNetNameTable);
3445 }
3446
3447 /* If we failed creating, quit */
3448 if (Status != STATUS_SUCCESS)
3449 {
3450 DPRINT1("RxFindOrConstructVirtualNetRoot() = Status: %x\n", Status);
3451 return Status;
3452 }
3453
3454 /* Otherwise, wait until the VNetRoot is stable */
3455 DPRINT("Waiting for stable condition for: %p\n", VNetRoot);
3456 RxWaitForStableVNetRoot(VNetRoot, RxContext);
3457 /* It's all good, update the RX_CONTEXT with all our structs */
3458 if (VNetRoot->Condition == Condition_Good)
3459 {
3460 PNET_ROOT NetRoot;
3461
3462 NetRoot = VNetRoot->NetRoot;
3463 RxContext->Create.pVNetRoot = (PMRX_V_NET_ROOT)VNetRoot;
3464 RxContext->Create.pNetRoot = (PMRX_NET_ROOT)NetRoot;
3465 RxContext->Create.pSrvCall = (PMRX_SRV_CALL)NetRoot->SrvCall;
3466 }
3467 else
3468 {
3469 RxDereferenceVNetRoot(VNetRoot, LHS_LockNotHeld);
3470 RxContext->Create.pVNetRoot = NULL;
3471 Status = STATUS_BAD_NETWORK_PATH;
3472 }
3473
3474 return Status;
3475 }
3476
3477 /*
3478 * @implemented
3479 */
3480 NTSTATUS
3481 RxFindOrCreateConnections(
3482 _In_ PRX_CONTEXT RxContext,
3483 _In_ PUNICODE_STRING CanonicalName,
3484 _In_ NET_ROOT_TYPE NetRootType,
3485 _Out_ PUNICODE_STRING LocalNetRootName,
3486 _Out_ PUNICODE_STRING FilePathName,
3487 _Inout_ PLOCK_HOLDING_STATE LockState,
3488 _In_ PRX_CONNECTION_ID RxConnectionId)
3489 {
3490 PVOID Container;
3491 PSRV_CALL SrvCall;
3492 PNET_ROOT NetRoot;
3493 PV_NET_ROOT VNetRoot;
3494 NTSTATUS Status = STATUS_UNSUCCESSFUL;
3495 PRX_PREFIX_TABLE PrefixTable;
3496 UNICODE_STRING RemainingName, NetRootName;
3497
3498 PAGED_CODE();
3499
3500 DPRINT("RxFindOrCreateConnections(%p, %wZ, %x, %p, %p, %p, %p)\n",
3501 RxContext, CanonicalName, NetRootType, LocalNetRootName,
3502 FilePathName, LockState, RxConnectionId);
3503
3504 *FilePathName = *CanonicalName;
3505 LocalNetRootName->Length = 0;
3506 LocalNetRootName->MaximumLength = 0;
3507 LocalNetRootName->Buffer = CanonicalName->Buffer;
3508
3509 /* UNC path, split it */
3510 if (FilePathName->Buffer[1] == ';')
3511 {
3512 BOOLEAN Slash;
3513 USHORT i, Length;
3514
3515 Slash = FALSE;
3516 for (i = 2; i < FilePathName->Length / sizeof(WCHAR); ++i)
3517 {
3518 if (FilePathName->Buffer[i] == OBJ_NAME_PATH_SEPARATOR)
3519 {
3520 Slash = TRUE;
3521 break;
3522 }
3523 }
3524
3525 if (!Slash)
3526 {
3527 return STATUS_OBJECT_NAME_INVALID;
3528 }
3529
3530 FilePathName->Buffer = &FilePathName->Buffer[i];
3531 Length = (USHORT)((ULONG_PTR)FilePathName->Buffer - (ULONG_PTR)LocalNetRootName->Buffer);
3532 LocalNetRootName->Length = Length;
3533 LocalNetRootName->MaximumLength = Length;
3534 FilePathName->Length -= Length;
3535
3536 DPRINT("CanonicalName: %wZ\n", CanonicalName);
3537 DPRINT(" -> FilePathName: %wZ\n", FilePathName);
3538 DPRINT(" -> LocalNetRootName: %wZ\n", LocalNetRootName);
3539 }
3540
3541 Container = NULL;
3542 PrefixTable = RxContext->RxDeviceObject->pRxNetNameTable;
3543
3544 _SEH2_TRY
3545 {
3546 RetryLookup:
3547 ASSERT(*LockState != LHS_LockNotHeld);
3548
3549 /* If previous lookup left something, dereference it */
3550 if (Container != NULL)
3551 {
3552 switch (NodeType(Container))
3553 {
3554 case RDBSS_NTC_SRVCALL:
3555 RxDereferenceSrvCall(Container, *LockState);
3556 break;
3557
3558 case RDBSS_NTC_NETROOT:
3559 RxDereferenceNetRoot(Container, *LockState);
3560 break;
3561
3562 case RDBSS_NTC_V_NETROOT:
3563 RxDereferenceVNetRoot(Container, *LockState);
3564 break;
3565
3566 default:
3567 /* Should never happen */
3568 ASSERT(FALSE);
3569 break;
3570 }
3571 }
3572
3573 /* Look for our NetRoot in prefix table */
3574 Container = RxPrefixTableLookupName(PrefixTable, FilePathName, &RemainingName, RxConnectionId);
3575 DPRINT("Container %p for path %wZ\n", Container, FilePathName);
3576
3577 while (TRUE)
3578 {
3579 UNICODE_STRING SrvCallName;
3580
3581 SrvCall = NULL;
3582 NetRoot = NULL;
3583 VNetRoot = NULL;
3584
3585 /* Assume we didn't succeed */
3586 RxContext->Create.pVNetRoot = NULL;
3587 RxContext->Create.pNetRoot = NULL;
3588 RxContext->Create.pSrvCall = NULL;
3589 RxContext->Create.Type = NetRootType;
3590
3591 /* If we found something */
3592 if (Container != NULL)
3593 {
3594 /* A VNetRoot */
3595 if (NodeType(Container) == RDBSS_NTC_V_NETROOT)
3596 {
3597 VNetRoot = Container;
3598 /* Use its NetRoot */
3599 NetRoot = VNetRoot->NetRoot;
3600
3601 /* If it's not stable, wait for it to be stable */
3602 if (NetRoot->Condition == Condition_InTransition)
3603 {
3604 RxReleasePrefixTableLock(PrefixTable);
3605 DPRINT("Waiting for stable condition for: %p\n", NetRoot);
3606 RxWaitForStableNetRoot(NetRoot, RxContext);
3607 RxAcquirePrefixTableLockExclusive(PrefixTable, TRUE);
3608 *LockState = LHS_ExclusiveLockHeld;
3609
3610 /* Now that's it's ok, retry lookup to find what we want */
3611 if (NetRoot->Condition == Condition_Good)
3612 {
3613 goto RetryLookup;
3614 }
3615 }
3616
3617 /* Is the associated netroot good? */
3618 if (NetRoot->Condition == Condition_Good)
3619 {
3620 SrvCall = (PSRV_CALL)NetRoot->pSrvCall;
3621
3622 /* If it is, and SrvCall as well, then, we have our active connection */
3623 if (SrvCall->Condition == Condition_Good &&
3624 SrvCall->RxDeviceObject == RxContext->RxDeviceObject)
3625 {
3626 RxContext->Create.pVNetRoot = (PMRX_V_NET_ROOT)VNetRoot;
3627 RxContext->Create.pNetRoot = (PMRX_NET_ROOT)NetRoot;
3628 RxContext->Create.pSrvCall = (PMRX_SRV_CALL)SrvCall;
3629
3630 Status = STATUS_CONNECTION_ACTIVE;
3631 _SEH2_LEAVE;
3632 }
3633 }
3634
3635 /* If VNetRoot was well constructed, it means the connection is active */
3636 if (VNetRoot->ConstructionStatus == STATUS_SUCCESS)
3637 {
3638 Status = STATUS_CONNECTION_ACTIVE;
3639 }
3640 else
3641 {
3642 Status = VNetRoot->ConstructionStatus;
3643 }
3644
3645 RxDereferenceVNetRoot(VNetRoot, *LockState);
3646 _SEH2_LEAVE;
3647 }
3648 /* Can only be a SrvCall */
3649 else
3650 {
3651 ASSERT(NodeType(Container) == RDBSS_NTC_SRVCALL);
3652 SrvCall = Container;
3653
3654 /* Wait for the SRV_CALL to be stable */
3655 if (SrvCall->Condition == Condition_InTransition)
3656 {
3657 RxReleasePrefixTableLock(PrefixTable);
3658 DPRINT("Waiting for stable condition for: %p\n", SrvCall);
3659 RxWaitForStableSrvCall(SrvCall, RxContext);
3660 RxAcquirePrefixTableLockExclusive(PrefixTable, TRUE);
3661 *LockState = LHS_ExclusiveLockHeld;
3662
3663 /* It went good, loop again to find what we look for */
3664 if (SrvCall->Condition == Condition_Good)
3665 {
3666 goto RetryLookup;
3667 }
3668 }
3669
3670 /* If it's not good... */
3671 if (SrvCall->Condition != Condition_Good)
3672 {
3673 /* But SRV_CALL was well constructed, assume a connection was active */
3674 if (SrvCall->Status == STATUS_SUCCESS)
3675 {
3676 Status = STATUS_CONNECTION_ACTIVE;
3677 }
3678 else
3679 {
3680 Status = SrvCall->Status;
3681 }
3682
3683 RxDereferenceSrvCall(SrvCall, *LockState);
3684 _SEH2_LEAVE;
3685 }
3686 }
3687 }
3688
3689 /* If we found a SRV_CALL not matching our DO, quit */
3690 if (SrvCall != NULL && SrvCall->Condition == Condition_Good &&
3691 SrvCall->RxDeviceObject != RxContext->RxDeviceObject)
3692 {
3693 RxDereferenceSrvCall(SrvCall, *LockState);
3694 Status = STATUS_BAD_NETWORK_NAME;
3695 _SEH2_LEAVE;
3696 }
3697
3698 /* Now, we want exclusive lock */
3699 if (*LockState == LHS_SharedLockHeld)
3700 {
3701 if (!RxAcquirePrefixTableLockExclusive(PrefixTable, FALSE))
3702 {
3703 RxReleasePrefixTableLock(PrefixTable);
3704 RxAcquirePrefixTableLockExclusive(PrefixTable, TRUE);
3705 *LockState = LHS_ExclusiveLockHeld;
3706 goto RetryLookup;
3707 }
3708
3709 RxReleasePrefixTableLock(PrefixTable);
3710 *LockState = LHS_ExclusiveLockHeld;
3711 }
3712
3713 ASSERT(*LockState == LHS_ExclusiveLockHeld);
3714
3715 /* If we reach that point, we found something, no need to create something */
3716 if (Container != NULL)
3717 {
3718 break;
3719 }
3720
3721 /* Get the name for the SRV_CALL */
3722 RxExtractServerName(FilePathName, &SrvCallName, NULL);
3723 DPRINT(" -> SrvCallName: %wZ\n", &SrvCallName);
3724 /* And create the SRV_CALL */
3725 SrvCall = RxCreateSrvCall(RxContext, &SrvCallName, NULL, RxConnectionId);
3726 if (SrvCall == NULL)
3727 {
3728 Status = STATUS_INSUFFICIENT_RESOURCES;
3729 _SEH2_LEAVE;
3730 }
3731
3732 /* Reset RX_CONTEXT, so far, connection creation isn't a success */
3733 RxReferenceSrvCall(SrvCall);
3734 RxContext->Create.pVNetRoot = NULL;
3735 RxContext->Create.pNetRoot = NULL;
3736 RxContext->Create.pSrvCall = NULL;
3737 RxContext->Create.Type = NetRootType;
3738 Container = SrvCall;
3739
3740 /* Construct SRV_CALL, ie, use mini-rdr */
3741 Status = RxConstructSrvCall(RxContext, SrvCall, LockState);
3742 ASSERT(Status != STATUS_SUCCESS || RxIsPrefixTableLockAcquired(PrefixTable));
3743 if (Status != STATUS_SUCCESS)
3744 {
3745 DPRINT1("RxConstructSrvCall() = Status: %x\n", Status);
3746 RxAcquirePrefixTableLockExclusive(PrefixTable, TRUE);
3747 RxDereferenceSrvCall(SrvCall, *LockState);
3748 RxReleasePrefixTableLock(PrefixTable);
3749 _SEH2_LEAVE;
3750 }
3751
3752 /* Loop again to make use of SRV_CALL stable condition wait */
3753 }
3754
3755 /* At that point, we have a stable SRV_CALL (either found or constructed) */
3756 ASSERT((NodeType(SrvCall) == RDBSS_NTC_SRVCALL) && (SrvCall->Condition == Condition_Good));
3757 ASSERT(NetRoot == NULL && VNetRoot == NULL);
3758 ASSERT(SrvCall->RxDeviceObject == RxContext->RxDeviceObject);
3759
3760 /* Call mini-rdr to get NetRoot name */
3761 SrvCall->RxDeviceObject->Dispatch->MRxExtractNetRootName(FilePathName, (PMRX_SRV_CALL)SrvCall, &NetRootName, NULL);
3762 /* And create the NetRoot with that name */
3763 NetRoot = RxCreateNetRoot(SrvCall, &NetRootName, 0, RxConnectionId);
3764 if (NetRoot == NULL)
3765 {
3766 Status = STATUS_INSUFFICIENT_RESOURCES;
3767 _SEH2_LEAVE;
3768 }
3769 NetRoot->Type = NetRootType;
3770
3771 RxDereferenceSrvCall(SrvCall, *LockState);
3772
3773 /* Finally, create the associated VNetRoot */
3774 VNetRoot = RxCreateVNetRoot(RxContext, NetRoot, CanonicalName, LocalNetRootName, FilePathName, RxConnectionId);
3775 if (VNetRoot == NULL)
3776 {
3777 RxFinalizeNetRoot(NetRoot, TRUE, TRUE);
3778 Status = STATUS_INSUFFICIENT_RESOURCES;
3779 _SEH2_LEAVE;
3780 }
3781 RxReferenceVNetRoot(VNetRoot);
3782
3783 /* We're get closer! */
3784 NetRoot->Condition = Condition_InTransition;
3785 RxContext->Create.pSrvCall = (PMRX_SRV_CALL)SrvCall;
3786 RxContext->Create.pNetRoot = (PMRX_NET_ROOT)NetRoot;
3787 RxContext->Create.pVNetRoot = (PMRX_V_NET_ROOT)VNetRoot;
3788
3789 /* Construct the NetRoot, involving the mini-rdr now that we have our three control structs */
3790 Status = RxConstructNetRoot(RxContext, SrvCall, NetRoot, VNetRoot, LockState);
3791 if (!NT_SUCCESS(Status))
3792 {
3793 RxTransitionVNetRoot(VNetRoot, Condition_Bad);
3794 DPRINT1("RxConstructNetRoot failed Ctxt: %p, VNet: %p, Status: %lx, Condition: %d\n", RxContext, VNetRoot, Status, VNetRoot->Condition);
3795 RxDereferenceVNetRoot(VNetRoot, *LockState);
3796
3797 RxContext->Create.pNetRoot = NULL;
3798 RxContext->Create.pVNetRoot = NULL;
3799 }
3800 else
3801 {
3802 PIO_STACK_LOCATION Stack;
3803
3804 ASSERT(*LockState == LHS_ExclusiveLockHeld);
3805
3806 Stack = RxContext->CurrentIrpSp;
3807 if (BooleanFlagOn(Stack->Parameters.Create.Options, FILE_CREATE_TREE_CONNECTION))
3808 {
3809 RxExclusivePrefixTableLockToShared(PrefixTable);
3810 *LockState = LHS_SharedLockHeld;
3811 }
3812 }
3813 }
3814 _SEH2_FINALLY
3815 {
3816 if (Status != STATUS_SUCCESS && Status != STATUS_CONNECTION_ACTIVE)
3817 {
3818 if (*LockState != LHS_LockNotHeld)
3819 {
3820 RxReleasePrefixTableLock(PrefixTable);
3821 *LockState = LHS_LockNotHeld;
3822 }
3823 }
3824 }
3825 _SEH2_END;
3826
3827 DPRINT("RxFindOrCreateConnections() = Status: %x\n", Status);
3828 return Status;
3829 }
3830
3831 /*
3832 * @implemented
3833 */
3834 VOID
3835 NTAPI
3836 RxFinishFcbInitialization(
3837 IN OUT PMRX_FCB Fcb,
3838 IN RX_FILE_TYPE FileType,
3839 IN PFCB_INIT_PACKET InitPacket OPTIONAL)
3840 {
3841 RX_FILE_TYPE OldType;
3842
3843 PAGED_CODE();
3844
3845 DPRINT("RxFinishFcbInitialization(%p, %x, %p)\n", Fcb, FileType, InitPacket);
3846
3847 OldType = NodeType(Fcb);
3848 NodeType(Fcb) = FileType;
3849 /* If mini-rdr already did the job for mailslot attributes, 0 the rest */
3850 if (BooleanFlagOn(Fcb->FcbState, FCB_STATE_TIME_AND_SIZE_ALREADY_SET) && FileType == RDBSS_NTC_MAILSLOT)
3851 {
3852 FILL_IN_FCB((PFCB)Fcb, 0, 0, 0, 0, 0, 0, 0, 0, 0);
3853 }
3854 /* Otherwise, if mini-rdr provided us with an init packet, copy its data */
<