[EXT2]
[reactos.git] / reactos / drivers / filesystems / ext2 / src / memory.c
1 /*
2 * COPYRIGHT: See COPYRIGHT.TXT
3 * PROJECT: Ext2 File System Driver for WinNT/2K/XP
4 * FILE: memory.c
5 * PROGRAMMER: Matt Wu <mattwu@163.com>
6 * HOMEPAGE: http://www.ext2fsd.com
7 * UPDATE HISTORY:
8 */
9
10 /* INCLUDES *****************************************************************/
11
12 #include "ext2fs.h"
13
14 /* GLOBALS ***************************************************************/
15
16 extern PEXT2_GLOBAL Ext2Global;
17
18 /* DEFINITIONS *************************************************************/
19
20 #ifdef ALLOC_PRAGMA
21 #pragma alloc_text(PAGE, Ext2AllocateFcb)
22 #pragma alloc_text(PAGE, Ext2FreeFcb)
23 #pragma alloc_text(PAGE, Ext2AllocateInode)
24 #pragma alloc_text(PAGE, Ext2DestroyInode)
25 #pragma alloc_text(PAGE, Ext2CheckBitmapConsistency)
26 #pragma alloc_text(PAGE, Ext2CheckSetBlock)
27 #pragma alloc_text(PAGE, Ext2InitializeVcb)
28 #pragma alloc_text(PAGE, Ext2FreeCcb)
29 #pragma alloc_text(PAGE, Ext2AllocateCcb)
30 #pragma alloc_text(PAGE, Ext2TearDownStream)
31 #pragma alloc_text(PAGE, Ext2DestroyVcb)
32 #pragma alloc_text(PAGE, Ext2SyncUninitializeCacheMap)
33 #pragma alloc_text(PAGE, Ext2ReaperThread)
34 #pragma alloc_text(PAGE, Ext2StartReaper)
35 #pragma alloc_text(PAGE, Ext2StopReaper)
36 #endif
37
38 PEXT2_IRP_CONTEXT
39 Ext2AllocateIrpContext (IN PDEVICE_OBJECT DeviceObject,
40 IN PIRP Irp )
41 {
42 PIO_STACK_LOCATION irpSp;
43 PEXT2_IRP_CONTEXT IrpContext;
44
45 ASSERT(DeviceObject != NULL);
46 ASSERT(Irp != NULL);
47
48 irpSp = IoGetCurrentIrpStackLocation(Irp);
49
50 IrpContext = (PEXT2_IRP_CONTEXT) (
51 ExAllocateFromNPagedLookasideList(
52 &(Ext2Global->Ext2IrpContextLookasideList)));
53
54 if (IrpContext == NULL) {
55 return NULL;
56 }
57
58 RtlZeroMemory(IrpContext, sizeof(EXT2_IRP_CONTEXT) );
59
60 IrpContext->Identifier.Type = EXT2ICX;
61 IrpContext->Identifier.Size = sizeof(EXT2_IRP_CONTEXT);
62
63 IrpContext->Irp = Irp;
64 IrpContext->MajorFunction = irpSp->MajorFunction;
65 IrpContext->MinorFunction = irpSp->MinorFunction;
66 IrpContext->DeviceObject = DeviceObject;
67 IrpContext->FileObject = irpSp->FileObject;
68 if (NULL != IrpContext->FileObject) {
69 IrpContext->Fcb = (PEXT2_FCB)IrpContext->FileObject->FsContext;
70 IrpContext->Ccb = (PEXT2_CCB)IrpContext->FileObject->FsContext2;
71 }
72
73 if (IrpContext->FileObject != NULL) {
74 IrpContext->RealDevice = IrpContext->FileObject->DeviceObject;
75 } else if (IrpContext->MajorFunction == IRP_MJ_FILE_SYSTEM_CONTROL) {
76 if (irpSp->Parameters.MountVolume.Vpb) {
77 IrpContext->RealDevice = irpSp->Parameters.MountVolume.Vpb->RealDevice;
78 }
79 }
80
81 if (IsFlagOn(irpSp->Flags, SL_WRITE_THROUGH)) {
82 SetFlag(IrpContext->Flags, IRP_CONTEXT_FLAG_WRITE_THROUGH);
83 }
84
85 if (IsFlagOn(irpSp->Flags, SL_OVERRIDE_VERIFY_VOLUME)) {
86 SetFlag(IrpContext->Flags, IRP_CONTEXT_FLAG_VERIFY_READ);
87 }
88
89 if (IrpContext->MajorFunction == IRP_MJ_CLEANUP ||
90 IrpContext->MajorFunction == IRP_MJ_CLOSE ||
91 IrpContext->MajorFunction == IRP_MJ_SHUTDOWN ||
92 IrpContext->MajorFunction == IRP_MJ_FILE_SYSTEM_CONTROL ||
93 IrpContext->MajorFunction == IRP_MJ_PNP ) {
94
95 if (IrpContext->MajorFunction == IRP_MJ_FILE_SYSTEM_CONTROL ||
96 IrpContext->MajorFunction == IRP_MJ_PNP) {
97 if (IoGetCurrentIrpStackLocation(Irp)->FileObject == NULL ||
98 IoIsOperationSynchronous(Irp)) {
99 SetFlag(IrpContext->Flags, IRP_CONTEXT_FLAG_WAIT);
100 }
101 } else {
102 SetFlag(IrpContext->Flags, IRP_CONTEXT_FLAG_WAIT);
103 }
104
105 } else if (IoIsOperationSynchronous(Irp)) {
106
107 SetFlag(IrpContext->Flags, IRP_CONTEXT_FLAG_WAIT);
108 }
109
110 IrpContext->IsTopLevel = (IoGetTopLevelIrp() == Irp);
111 IrpContext->ExceptionInProgress = FALSE;
112 INC_IRP_COUNT(IrpContext);
113
114 return IrpContext;
115 }
116
117 VOID
118 Ext2FreeIrpContext (IN PEXT2_IRP_CONTEXT IrpContext)
119 {
120 ASSERT(IrpContext != NULL);
121
122 ASSERT((IrpContext->Identifier.Type == EXT2ICX) &&
123 (IrpContext->Identifier.Size == sizeof(EXT2_IRP_CONTEXT)));
124
125 /* free the IrpContext to NonPagedList */
126 IrpContext->Identifier.Type = 0;
127 IrpContext->Identifier.Size = 0;
128
129 DEC_IRP_COUNT(IrpContext);
130 ExFreeToNPagedLookasideList(&(Ext2Global->Ext2IrpContextLookasideList), IrpContext);
131 }
132
133
134 PEXT2_FCB
135 Ext2AllocateFcb (
136 IN PEXT2_VCB Vcb,
137 IN PEXT2_MCB Mcb
138 )
139 {
140 PEXT2_FCB Fcb;
141
142 Fcb = (PEXT2_FCB) ExAllocateFromNPagedLookasideList(
143 &(Ext2Global->Ext2FcbLookasideList));
144
145 if (!Fcb) {
146 return NULL;
147 }
148
149 RtlZeroMemory(Fcb, sizeof(EXT2_FCB));
150 Fcb->Identifier.Type = EXT2FCB;
151 Fcb->Identifier.Size = sizeof(EXT2_FCB);
152
153 #ifndef _WIN2K_TARGET_
154 ExInitializeFastMutex(&Fcb->Mutex);
155 FsRtlSetupAdvancedHeader(&Fcb->Header, &Fcb->Mutex);
156 #endif
157
158 FsRtlInitializeOplock(&Fcb->Oplock);
159 FsRtlInitializeFileLock (
160 &Fcb->FileLockAnchor,
161 NULL,
162 NULL );
163
164 Fcb->OpenHandleCount = 0;
165 Fcb->ReferenceCount = 0;
166 Fcb->Vcb = Vcb;
167 Fcb->Inode = &Mcb->Inode;
168
169 ASSERT(Mcb->Fcb == NULL);
170 Ext2ReferMcb(Mcb);
171 Fcb->Mcb = Mcb;
172 Mcb->Fcb = Fcb;
173
174 DEBUG(DL_RES, ("Ext2AllocateFcb: Fcb %p created: %wZ.\n",
175 Fcb, &Fcb->Mcb->FullName));
176
177 RtlZeroMemory(&Fcb->Header, sizeof(FSRTL_COMMON_FCB_HEADER));
178 Fcb->Header.NodeTypeCode = (USHORT) EXT2FCB;
179 Fcb->Header.NodeByteSize = sizeof(EXT2_FCB);
180 Fcb->Header.IsFastIoPossible = FastIoIsNotPossible;
181 Fcb->Header.Resource = &(Fcb->MainResource);
182 Fcb->Header.PagingIoResource = &(Fcb->PagingIoResource);
183
184 Fcb->Header.FileSize.QuadPart = Mcb->Inode.i_size;
185 Fcb->Header.ValidDataLength.QuadPart = Mcb->Inode.i_size;
186 Fcb->Header.AllocationSize.QuadPart = CEILING_ALIGNED(ULONGLONG,
187 Fcb->Header.FileSize.QuadPart, (ULONGLONG)Vcb->BlockSize);
188
189 Fcb->SectionObject.DataSectionObject = NULL;
190 Fcb->SectionObject.SharedCacheMap = NULL;
191 Fcb->SectionObject.ImageSectionObject = NULL;
192
193 ExInitializeResourceLite(&(Fcb->MainResource));
194 ExInitializeResourceLite(&(Fcb->PagingIoResource));
195
196 Ext2InsertFcb(Vcb, Fcb);
197
198 INC_MEM_COUNT(PS_FCB, Fcb, sizeof(EXT2_FCB));
199
200 return Fcb;
201 }
202
203 VOID
204 Ext2FreeFcb (IN PEXT2_FCB Fcb)
205 {
206 PEXT2_VCB Vcb = Fcb->Vcb;
207
208 ASSERT((Fcb != NULL) && (Fcb->Identifier.Type == EXT2FCB) &&
209 (Fcb->Identifier.Size == sizeof(EXT2_FCB)));
210 ASSERT((Fcb->Mcb->Identifier.Type == EXT2MCB) &&
211 (Fcb->Mcb->Identifier.Size == sizeof(EXT2_MCB)));
212
213 #ifndef _WIN2K_TARGET_
214 FsRtlTeardownPerStreamContexts(&Fcb->Header);
215 #endif
216
217 if ((Fcb->Mcb->Identifier.Type == EXT2MCB) &&
218 (Fcb->Mcb->Identifier.Size == sizeof(EXT2_MCB))) {
219
220 ASSERT (Fcb->Mcb->Fcb == Fcb);
221 if (IsMcbSpecialFile(Fcb->Mcb) || IsFileDeleted(Fcb->Mcb)) {
222
223 ASSERT(!IsRoot(Fcb));
224 Ext2RemoveMcb(Fcb->Vcb, Fcb->Mcb);
225 Fcb->Mcb->Fcb = NULL;
226
227 Ext2UnlinkMcb(Vcb, Fcb->Mcb);
228 Ext2DerefMcb(Fcb->Mcb);
229 Ext2LinkHeadMcb(Vcb, Fcb->Mcb);
230
231 } else {
232
233 Fcb->Mcb->Fcb = NULL;
234 Ext2DerefMcb(Fcb->Mcb);
235 }
236
237 } else {
238 DbgBreak();
239 }
240
241 Ext2RemoveFcb(Fcb->Vcb, Fcb);
242
243 FsRtlUninitializeFileLock(&Fcb->FileLockAnchor);
244 FsRtlUninitializeOplock(&Fcb->Oplock);
245 ExDeleteResourceLite(&Fcb->MainResource);
246 ExDeleteResourceLite(&Fcb->PagingIoResource);
247
248 DEBUG(DL_RES, ( "Ext2FreeFcb: Fcb (%p) is being released: %wZ.\n",
249 Fcb, &Fcb->Mcb->FullName));
250
251 Fcb->Identifier.Type = 0;
252 Fcb->Identifier.Size = 0;
253
254 ExFreeToNPagedLookasideList(&(Ext2Global->Ext2FcbLookasideList), Fcb);
255 DEC_MEM_COUNT(PS_FCB, Fcb, sizeof(EXT2_FCB));
256 }
257
258 /* Insert Fcb to Vcb->FcbList queue */
259
260 VOID
261 Ext2InsertFcb(PEXT2_VCB Vcb, PEXT2_FCB Fcb)
262 {
263 ExInterlockedInsertTailList(&Vcb->FcbList, &Fcb->Next, &Vcb->FcbLock);
264 Ext2ReferXcb(&Vcb->FcbCount);
265 }
266
267 /* Remove Fcb from Vcb->FcbList queue */
268
269 VOID
270 Ext2RemoveFcb(PEXT2_VCB Vcb, PEXT2_FCB Fcb)
271 {
272 KIRQL irql;
273
274 KeAcquireSpinLock(&Vcb->FcbLock, &irql);
275 RemoveEntryList(&(Fcb->Next));
276 if (Vcb->FcbCount > 0) {
277 Ext2DerefXcb(&Vcb->FcbCount);
278 } else {
279 DbgBreak();
280 }
281 KeReleaseSpinLock(&Vcb->FcbLock, irql);
282 }
283
284 PEXT2_CCB
285 Ext2AllocateCcb (ULONG Flags, PEXT2_MCB SymLink)
286 {
287 PEXT2_CCB Ccb;
288
289 Ccb = (PEXT2_CCB) (ExAllocateFromNPagedLookasideList(
290 &(Ext2Global->Ext2CcbLookasideList)));
291 if (!Ccb) {
292 return NULL;
293 }
294
295 DEBUG(DL_RES, ( "ExtAllocateCcb: Ccb created: %ph.\n", Ccb));
296
297 RtlZeroMemory(Ccb, sizeof(EXT2_CCB));
298
299 Ccb->Identifier.Type = EXT2CCB;
300 Ccb->Identifier.Size = sizeof(EXT2_CCB);
301 Ccb->Flags = Flags;
302
303 Ccb->SymLink = SymLink;
304 if (SymLink) {
305 ASSERT(SymLink->Refercount > 0);
306 Ext2ReferMcb(SymLink);
307 DEBUG(DL_INF, ( "ExtAllocateCcb: Ccb SymLink: %wZ.\n",
308 &Ccb->SymLink->FullName));
309 }
310
311 Ccb->DirectorySearchPattern.Length = 0;
312 Ccb->DirectorySearchPattern.MaximumLength = 0;
313 Ccb->DirectorySearchPattern.Buffer = 0;
314
315 INC_MEM_COUNT(PS_CCB, Ccb, sizeof(EXT2_CCB));
316
317 return Ccb;
318 }
319
320 VOID
321 Ext2FreeCcb (IN PEXT2_VCB Vcb, IN PEXT2_CCB Ccb)
322 {
323 ASSERT(Ccb != NULL);
324
325 ASSERT((Ccb->Identifier.Type == EXT2CCB) &&
326 (Ccb->Identifier.Size == sizeof(EXT2_CCB)));
327
328 DEBUG(DL_RES, ( "Ext2FreeCcb: Ccb = %ph.\n", Ccb));
329
330 if (Ccb->SymLink) {
331 DEBUG(DL_INF, ( "Ext2FreeCcb: Ccb SymLink: %wZ.\n",
332 &Ccb->SymLink->FullName));
333 if (IsFileDeleted(Ccb->SymLink->Target)) {
334 Ext2UnlinkMcb(Vcb, Ccb->SymLink);
335 Ext2DerefMcb(Ccb->SymLink);
336 Ext2LinkHeadMcb(Vcb, Ccb->SymLink);
337 } else {
338 Ext2DerefMcb(Ccb->SymLink);
339 }
340 }
341
342 if (Ccb->DirectorySearchPattern.Buffer != NULL) {
343 DEC_MEM_COUNT(PS_DIR_PATTERN, Ccb->DirectorySearchPattern.Buffer,
344 Ccb->DirectorySearchPattern.MaximumLength );
345 Ext2FreePool(Ccb->DirectorySearchPattern.Buffer, EXT2_DIRSP_MAGIC);
346 }
347
348 ExFreeToNPagedLookasideList(&(Ext2Global->Ext2CcbLookasideList), Ccb);
349 DEC_MEM_COUNT(PS_CCB, Ccb, sizeof(EXT2_CCB));
350 }
351
352 PEXT2_INODE
353 Ext2AllocateInode (PEXT2_VCB Vcb)
354 {
355 PVOID inode = NULL;
356
357 inode = ExAllocateFromNPagedLookasideList(
358 &(Vcb->InodeLookasideList));
359 if (!inode) {
360 return NULL;
361 }
362
363 RtlZeroMemory(inode, INODE_SIZE);
364
365 DEBUG(DL_INF, ("ExtAllocateInode: Inode created: %ph.\n", inode));
366 INC_MEM_COUNT(PS_EXT2_INODE, inode, INODE_SIZE);
367
368 return inode;
369 }
370
371 VOID
372 Ext2DestroyInode (IN PEXT2_VCB Vcb, IN PEXT2_INODE inode)
373 {
374 ASSERT(inode != NULL);
375
376 DEBUG(DL_INF, ("Ext2FreeInode: Inode = %ph.\n", inode));
377
378 ExFreeToNPagedLookasideList(&(Vcb->InodeLookasideList), inode);
379 DEC_MEM_COUNT(PS_EXT2_INODE, inode, INODE_SIZE);
380 }
381
382 struct dentry * Ext2AllocateEntry()
383 {
384 struct dentry *de;
385
386 de = (struct dentry *)ExAllocateFromNPagedLookasideList(
387 &(Ext2Global->Ext2DentryLookasideList));
388 if (!de) {
389 return NULL;
390 }
391
392 RtlZeroMemory(de, sizeof(struct dentry));
393 INC_MEM_COUNT(PS_DENTRY, de, sizeof(struct dentry));
394
395 return de;
396 }
397
398 VOID Ext2FreeEntry (IN struct dentry *de)
399 {
400 ASSERT(de != NULL);
401
402 if (de->d_name.name)
403 ExFreePool(de->d_name.name);
404
405 ExFreeToNPagedLookasideList(&(Ext2Global->Ext2DentryLookasideList), de);
406 DEC_MEM_COUNT(PS_DENTRY, de, sizeof(struct dentry));
407 }
408
409
410 struct dentry *Ext2BuildEntry(PEXT2_VCB Vcb, PEXT2_MCB Dcb, PUNICODE_STRING FileName)
411 {
412 OEM_STRING Oem = { 0 };
413 struct dentry *de = NULL;
414 NTSTATUS Status = STATUS_INSUFFICIENT_RESOURCES;
415
416 _SEH2_TRY {
417
418 de = Ext2AllocateEntry();
419 if (!de) {
420 DEBUG(DL_ERR, ("Ext2BuildEntry: failed to allocate dentry.\n"));
421 _SEH2_LEAVE;
422 }
423 de->d_sb = &Vcb->sb;
424 if (Dcb)
425 de->d_parent = Dcb->de;
426
427 Oem.MaximumLength = (USHORT)Ext2UnicodeToOEMSize(Vcb, FileName) + 1;
428 Oem.Buffer = ExAllocatePool(PagedPool, Oem.MaximumLength);
429 if (!Oem.Buffer) {
430 DEBUG(DL_ERR, ( "Ex2BuildEntry: failed to allocate OEM name.\n"));
431 _SEH2_LEAVE;
432 }
433 de->d_name.name = Oem.Buffer;
434 RtlZeroMemory(Oem.Buffer, Oem.MaximumLength);
435 Status = Ext2UnicodeToOEM(Vcb, &Oem, FileName);
436 if (!NT_SUCCESS(Status)) {
437 DEBUG(DL_CP, ("Ext2BuildEntry: failed to convert %S to OEM.\n", FileName->Buffer));
438 _SEH2_LEAVE;
439 }
440 de->d_name.len = Oem.Length;
441
442 } _SEH2_FINALLY {
443
444 if (!NT_SUCCESS(Status)) {
445 if (de)
446 Ext2FreeEntry(de);
447 }
448 } _SEH2_END;
449
450 return de;
451 }
452
453 PEXT2_EXTENT
454 Ext2AllocateExtent ()
455 {
456 PEXT2_EXTENT Extent;
457
458 Extent = (PEXT2_EXTENT)ExAllocateFromNPagedLookasideList(
459 &(Ext2Global->Ext2ExtLookasideList));
460 if (!Extent) {
461 return NULL;
462 }
463
464 RtlZeroMemory(Extent, sizeof(EXT2_EXTENT));
465 INC_MEM_COUNT(PS_EXTENT, Extent, sizeof(EXT2_EXTENT));
466
467 return Extent;
468 }
469
470 VOID
471 Ext2FreeExtent (IN PEXT2_EXTENT Extent)
472 {
473 ASSERT(Extent != NULL);
474 ExFreeToNPagedLookasideList(&(Ext2Global->Ext2ExtLookasideList), Extent);
475 DEC_MEM_COUNT(PS_EXTENT, Extent, sizeof(EXT2_EXTENT));
476 }
477
478 ULONG
479 Ext2CountExtents(IN PEXT2_EXTENT Chain)
480 {
481 ULONG count = 0;
482 PEXT2_EXTENT List = Chain;
483
484 while (List) {
485 count += 1;
486 List = List->Next;
487 }
488
489 return count;
490 }
491
492 VOID
493 Ext2JointExtents(
494 IN PEXT2_EXTENT Chain,
495 IN PEXT2_EXTENT Extent
496 )
497 {
498 #ifndef __REACTOS__
499 ULONG count = 0;
500 #endif
501 PEXT2_EXTENT List = Chain;
502
503 while (List->Next) {
504 List = List->Next;
505 }
506
507 List->Next = Extent;
508 }
509
510
511 VOID
512 Ext2DestroyExtentChain(IN PEXT2_EXTENT Chain)
513 {
514 PEXT2_EXTENT Extent = NULL, List = Chain;
515
516 while (List) {
517 Extent = List->Next;
518 Ext2FreeExtent(List);
519 List = Extent;
520 }
521 }
522
523 BOOLEAN
524 Ext2ListExtents(PLARGE_MCB Extents)
525 {
526 if (FsRtlNumberOfRunsInLargeMcb(Extents) != 0) {
527
528 LONGLONG DirtyVba;
529 LONGLONG DirtyLba;
530 LONGLONG DirtyLength;
531 int i, n = 0;
532
533 for (i = 0; FsRtlGetNextLargeMcbEntry(
534 Extents, i, &DirtyVba,
535 &DirtyLba, &DirtyLength); i++) {
536 if (DirtyVba > 0 && DirtyLba != -1) {
537 DEBUG(DL_EXT, ("Vba:%I64xh Lba:%I64xh Len:%I64xh.\n", DirtyVba, DirtyLba, DirtyLength));
538 n++;
539 }
540 }
541
542 return n ? TRUE : FALSE;
543 }
544
545 return FALSE;
546 }
547
548 VOID
549 Ext2CheckExtent(
550 PLARGE_MCB Zone,
551 LONGLONG Vbn,
552 LONGLONG Lbn,
553 LONGLONG Length,
554 BOOLEAN bAdded
555 )
556 {
557 #if EXT2_DEBUG
558 LONGLONG DirtyLbn;
559 LONGLONG DirtyLen;
560 LONGLONG RunStart;
561 LONGLONG RunLength;
562 ULONG Index;
563 BOOLEAN bFound = FALSE;
564
565 bFound = FsRtlLookupLargeMcbEntry(
566 Zone,
567 Vbn,
568 &DirtyLbn,
569 &DirtyLen,
570 &RunStart,
571 &RunLength,
572 &Index );
573
574 if (!bAdded && (!bFound || DirtyLbn == -1)) {
575 return;
576 }
577
578 if ( !bFound || (DirtyLbn == -1) ||
579 (DirtyLbn != Lbn) ||
580 (DirtyLen < Length)) {
581
582 DbgBreak();
583
584 for (Index = 0; TRUE; Index++) {
585
586 if (!FsRtlGetNextLargeMcbEntry(
587 Zone,
588 Index,
589 &Vbn,
590 &Lbn,
591 &Length)) {
592 break;
593 }
594
595 DEBUG(DL_EXT, ("Index = %xh Vbn = %I64xh Lbn = %I64xh Len = %I64xh\n",
596 Index, Vbn, Lbn, Length ));
597 }
598 }
599 #endif
600 }
601
602 VOID
603 Ext2ClearAllExtents(PLARGE_MCB Zone)
604 {
605 _SEH2_TRY {
606 FsRtlTruncateLargeMcb(Zone, (LONGLONG)0);
607 } _SEH2_EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
608 DbgBreak();
609 } _SEH2_END;
610 }
611
612
613 BOOLEAN
614 Ext2AddVcbExtent (
615 IN PEXT2_VCB Vcb,
616 IN LONGLONG Vbn,
617 IN LONGLONG Length
618 )
619 {
620 ULONG TriedTimes = 0;
621
622 LONGLONG Offset = 0;
623 BOOLEAN rc = FALSE;
624
625 Offset = Vbn & (~(Vcb->IoUnitSize - 1));
626 Length = (Vbn - Offset + Length + Vcb->IoUnitSize - 1) &
627 ~(Vcb->IoUnitSize - 1);
628
629 ASSERT ((Offset & (Vcb->IoUnitSize - 1)) == 0);
630 ASSERT ((Length & (Vcb->IoUnitSize - 1)) == 0);
631
632 Offset = (Offset >> Vcb->IoUnitBits) + 1;
633 Length = (Length >> Vcb->IoUnitBits);
634
635 Again:
636
637 _SEH2_TRY {
638 rc = FsRtlAddLargeMcbEntry(
639 &Vcb->Extents,
640 Offset,
641 Offset,
642 Length
643 );
644 } _SEH2_EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
645 DbgBreak();
646 rc = FALSE;
647 } _SEH2_END;
648
649 if (!rc && ++TriedTimes < 10) {
650 Ext2Sleep(TriedTimes * 100);
651 goto Again;
652 }
653
654 DEBUG(DL_EXT, ("Ext2AddVcbExtent: Vbn=%I64xh Length=%I64xh,"
655 " rc=%d Runs=%u\n", Offset, Length, rc,
656 FsRtlNumberOfRunsInLargeMcb(&Vcb->Extents)));
657
658 if (rc) {
659 Ext2CheckExtent(&Vcb->Extents, Offset, Offset, Length, TRUE);
660 }
661
662 return rc;
663 }
664
665 BOOLEAN
666 Ext2RemoveVcbExtent (
667 IN PEXT2_VCB Vcb,
668 IN LONGLONG Vbn,
669 IN LONGLONG Length
670 )
671 {
672 ULONG TriedTimes = 0;
673 LONGLONG Offset = 0;
674 BOOLEAN rc = TRUE;
675
676 Offset = Vbn & (~(Vcb->IoUnitSize - 1));
677 Length = (Length + Vbn - Offset + Vcb->IoUnitSize - 1) & (~(Vcb->IoUnitSize - 1));
678
679 ASSERT ((Offset & (Vcb->IoUnitSize - 1)) == 0);
680 ASSERT ((Length & (Vcb->IoUnitSize - 1)) == 0);
681
682 Offset = (Offset >> Vcb->IoUnitBits) + 1;
683 Length = (Length >> Vcb->IoUnitBits);
684
685 Again:
686
687 _SEH2_TRY {
688 FsRtlRemoveLargeMcbEntry(
689 &Vcb->Extents,
690 Offset,
691 Length
692 );
693 } _SEH2_EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
694 DbgBreak();
695 rc = FALSE;
696 } _SEH2_END;
697
698 if (!rc && ++TriedTimes < 10) {
699 Ext2Sleep(TriedTimes * 100);
700 goto Again;
701 }
702
703 DEBUG(DL_EXT, ("Ext2RemoveVcbExtent: Vbn=%I64xh Length=%I64xh Runs=%u\n",
704 Offset, Length, FsRtlNumberOfRunsInLargeMcb(&Vcb->Extents)));
705 if (rc) {
706 Ext2CheckExtent(&Vcb->Extents, Offset, 0, Length, FALSE);
707 }
708
709 return rc;
710 }
711
712 BOOLEAN
713 Ext2LookupVcbExtent (
714 IN PEXT2_VCB Vcb,
715 IN LONGLONG Vbn,
716 OUT PLONGLONG Lbn,
717 OUT PLONGLONG Length
718 )
719 {
720 LONGLONG offset;
721 BOOLEAN rc;
722
723 offset = Vbn & (~(Vcb->IoUnitSize - 1));
724 ASSERT ((offset & (Vcb->IoUnitSize - 1)) == 0);
725 offset = (offset >> Vcb->IoUnitBits) + 1;
726
727 rc = FsRtlLookupLargeMcbEntry(
728 &(Vcb->Extents),
729 offset,
730 Lbn,
731 Length,
732 NULL,
733 NULL,
734 NULL
735 );
736
737 if (rc) {
738
739 if (Lbn && ((*Lbn) != -1)) {
740 ASSERT((*Lbn) > 0);
741 (*Lbn) = (((*Lbn) - 1) << Vcb->IoUnitBits);
742 (*Lbn) += ((Vbn) & (Vcb->IoUnitSize - 1));
743 }
744
745 if (Length && *Length) {
746 (*Length) <<= Vcb->IoUnitBits;
747 (*Length) -= ((Vbn) & (Vcb->IoUnitSize - 1));
748 }
749 }
750
751 return rc;
752 }
753
754
755 BOOLEAN
756 Ext2AddMcbExtent (
757 IN PEXT2_VCB Vcb,
758 IN PEXT2_MCB Mcb,
759 IN LONGLONG Vbn,
760 IN LONGLONG Lbn,
761 IN LONGLONG Length
762 )
763 {
764 ULONG TriedTimes = 0;
765 LONGLONG Base = 0;
766 UCHAR Bits = 0;
767 BOOLEAN rc = FALSE;
768
769 Base = (LONGLONG)BLOCK_SIZE;
770 Bits = (UCHAR)BLOCK_BITS;
771
772 ASSERT ((Vbn & (Base - 1)) == 0);
773 ASSERT ((Lbn & (Base - 1)) == 0);
774 ASSERT ((Length & (Base - 1)) == 0);
775
776 Vbn = (Vbn >> Bits) + 1;
777 Lbn = (Lbn >> Bits) + 1;
778 Length = (Length >> Bits);
779
780 Again:
781
782 _SEH2_TRY {
783
784 rc = FsRtlAddLargeMcbEntry(
785 &Mcb->Extents,
786 Vbn,
787 Lbn,
788 Length
789 );
790
791 } _SEH2_EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
792
793 DbgBreak();
794 rc = FALSE;
795 } _SEH2_END;
796
797 if (!rc && ++TriedTimes < 10) {
798 Ext2Sleep(TriedTimes * 100);
799 goto Again;
800 }
801
802 DEBUG(DL_EXT, ("Ext2AddMcbExtent: Vbn=%I64xh Lbn=%I64xh Length=%I64xh,"
803 " rc=%d Runs=%u\n", Vbn, Lbn, Length, rc,
804 FsRtlNumberOfRunsInLargeMcb(&Mcb->Extents)));
805
806 if (rc) {
807 Ext2CheckExtent(&Mcb->Extents, Vbn, Lbn, Length, TRUE);
808 }
809
810 return rc;
811 }
812
813 BOOLEAN
814 Ext2RemoveMcbExtent (
815 IN PEXT2_VCB Vcb,
816 IN PEXT2_MCB Mcb,
817 IN LONGLONG Vbn,
818 IN LONGLONG Length
819 )
820 {
821 ULONG TriedTimes = 0;
822 LONGLONG Base = 0;
823 UCHAR Bits = 0;
824 BOOLEAN rc = TRUE;
825
826 Base = (LONGLONG)BLOCK_SIZE;
827 Bits = (UCHAR)BLOCK_BITS;
828
829 ASSERT ((Vbn & (Base - 1)) == 0);
830 ASSERT ((Length & (Base - 1)) == 0);
831
832 Vbn = (Vbn >> Bits) + 1;
833 Length = (Length >> Bits);
834
835 Again:
836
837 _SEH2_TRY {
838 FsRtlRemoveLargeMcbEntry(
839 &Mcb->Extents,
840 Vbn,
841 Length
842 );
843 } _SEH2_EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
844 DbgBreak();
845 rc = FALSE;
846 } _SEH2_END;
847
848 if (!rc && ++TriedTimes < 10) {
849 Ext2Sleep(TriedTimes * 100);
850 goto Again;
851 }
852
853 DEBUG(DL_EXT, ("Ext2RemoveMcbExtent: Vbn=%I64xh Length=%I64xh Runs=%u\n",
854 Vbn, Length, FsRtlNumberOfRunsInLargeMcb(&Mcb->Extents)));
855 if (rc) {
856 Ext2CheckExtent(&Mcb->Extents, Vbn, 0, Length, FALSE);
857 }
858
859 return rc;
860 }
861
862 BOOLEAN
863 Ext2LookupMcbExtent (
864 IN PEXT2_VCB Vcb,
865 IN PEXT2_MCB Mcb,
866 IN LONGLONG Vbn,
867 OUT PLONGLONG Lbn,
868 OUT PLONGLONG Length
869 )
870 {
871 LONGLONG offset;
872 BOOLEAN rc;
873
874 offset = Vbn & (~((LONGLONG)BLOCK_SIZE - 1));
875 ASSERT ((offset & (BLOCK_SIZE - 1)) == 0);
876 offset = (offset >> BLOCK_BITS) + 1;
877
878 rc = FsRtlLookupLargeMcbEntry(
879 &(Mcb->Extents),
880 offset,
881 Lbn,
882 Length,
883 NULL,
884 NULL,
885 NULL
886 );
887
888 if (rc) {
889
890 if (Lbn && ((*Lbn) != -1)) {
891 ASSERT((*Lbn) > 0);
892 (*Lbn) = (((*Lbn) - 1) << BLOCK_BITS);
893 (*Lbn) += ((Vbn) & ((LONGLONG)BLOCK_SIZE - 1));
894 }
895
896 if (Length && *Length) {
897 (*Length) <<= BLOCK_BITS;
898 (*Length) -= ((Vbn) & ((LONGLONG)BLOCK_SIZE - 1));
899 }
900 }
901
902 return rc;
903 }
904
905
906 BOOLEAN
907 Ext2AddMcbMetaExts (
908 IN PEXT2_VCB Vcb,
909 IN PEXT2_MCB Mcb,
910 IN ULONG Block,
911 IN ULONG Length
912 )
913 {
914 ULONG TriedTimes = 0;
915 LONGLONG Lbn = Block + 1;
916 BOOLEAN rc = TRUE;
917
918 Again:
919
920 _SEH2_TRY {
921
922 rc = FsRtlAddLargeMcbEntry(
923 &Mcb->MetaExts,
924 Lbn,
925 Lbn,
926 Length
927 );
928
929 } _SEH2_EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
930
931 DbgBreak();
932 rc = FALSE;
933 } _SEH2_END;
934
935 if (!rc && ++TriedTimes < 10) {
936 Ext2Sleep(TriedTimes * 100);
937 goto Again;
938 }
939
940 DEBUG(DL_EXT, ("Ext2AddMcbMetaExts: Block: %xh-%xh rc=%d Runs=%u\n", Block,
941 Length, rc, FsRtlNumberOfRunsInLargeMcb(&Mcb->MetaExts)));
942
943 if (rc) {
944 Ext2CheckExtent(&Mcb->MetaExts, Lbn, Lbn, Length, TRUE);
945 }
946
947 return rc;
948 }
949
950 BOOLEAN
951 Ext2RemoveMcbMetaExts (
952 IN PEXT2_VCB Vcb,
953 IN PEXT2_MCB Mcb,
954 IN ULONG Block,
955 IN ULONG Length
956 )
957 {
958 ULONG TriedTimes = 0;
959 LONGLONG Lbn = Block + 1;
960 BOOLEAN rc = TRUE;
961
962 Again:
963
964 _SEH2_TRY {
965
966 FsRtlRemoveLargeMcbEntry(
967 &Mcb->MetaExts,
968 Lbn,
969 Length
970 );
971
972 } _SEH2_EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
973 DbgBreak();
974 rc = FALSE;
975 } _SEH2_END;
976
977 if (!rc && ++TriedTimes < 10) {
978 Ext2Sleep(TriedTimes * 100);
979 goto Again;
980 }
981
982 DEBUG(DL_EXT, ("Ext2RemoveMcbMetaExts: Block: %xh-%xhh Runs=%u\n", Block,
983 Length, FsRtlNumberOfRunsInLargeMcb(&Mcb->MetaExts)));
984 if (rc) {
985 Ext2CheckExtent(&Mcb->MetaExts, Lbn, 0, Length, FALSE);
986 }
987
988 return rc;
989 }
990
991
992 BOOLEAN
993 Ext2AddBlockExtent(
994 IN PEXT2_VCB Vcb,
995 IN PEXT2_MCB Mcb,
996 IN ULONG Start,
997 IN ULONG Block,
998 IN ULONG Number
999 )
1000 {
1001 LONGLONG Vbn = 0;
1002 LONGLONG Lbn = 0;
1003 LONGLONG Length = 0;
1004
1005 Vbn = ((LONGLONG) Start) << BLOCK_BITS;
1006 Lbn = ((LONGLONG) Block) << BLOCK_BITS;
1007 Length = ((LONGLONG)Number << BLOCK_BITS);
1008
1009 if (Mcb) {
1010 #if EXT2_DEBUG
1011 ULONG _block = 0, _mapped = 0;
1012 BOOLEAN _rc = Ext2LookupBlockExtent(Vcb, Mcb, Start, &_block, &_mapped);
1013 if (_rc && _block != 0 && (_block != Block)) {
1014 DbgBreak();
1015 }
1016 #endif
1017 return Ext2AddMcbExtent(Vcb, Mcb, Vbn, Lbn, Length);
1018
1019 }
1020
1021 ASSERT(Start == Block);
1022 return Ext2AddVcbExtent(Vcb, Vbn, Length);
1023 }
1024
1025
1026 BOOLEAN
1027 Ext2LookupBlockExtent(
1028 IN PEXT2_VCB Vcb,
1029 IN PEXT2_MCB Mcb,
1030 IN ULONG Start,
1031 IN PULONG Block,
1032 IN PULONG Mapped
1033 )
1034 {
1035 LONGLONG Vbn = 0;
1036 LONGLONG Lbn = 0;
1037 LONGLONG Length = 0;
1038
1039 BOOLEAN rc = FALSE;
1040
1041 Vbn = ((LONGLONG) Start) << BLOCK_BITS;
1042
1043 if (Mcb) {
1044 rc = Ext2LookupMcbExtent(Vcb, Mcb, Vbn, &Lbn, &Length);
1045 } else {
1046 rc = Ext2LookupVcbExtent(Vcb, Vbn, &Lbn, &Length);
1047 }
1048
1049 if (rc) {
1050 *Mapped = (ULONG)(Length >> BLOCK_BITS);
1051 if (Lbn != -1 && Length > 0) {
1052 *Block = (ULONG)(Lbn >> BLOCK_BITS);
1053 } else {
1054 *Block = 0;
1055 }
1056 }
1057
1058 return rc;
1059 }
1060
1061
1062 BOOLEAN
1063 Ext2RemoveBlockExtent(
1064 IN PEXT2_VCB Vcb,
1065 IN PEXT2_MCB Mcb,
1066 IN ULONG Start,
1067 IN ULONG Number
1068 )
1069 {
1070 LONGLONG Vbn = 0;
1071 LONGLONG Length = 0;
1072 BOOLEAN rc;
1073
1074 Vbn = ((LONGLONG) Start) << BLOCK_BITS;
1075 Length = ((LONGLONG)Number << BLOCK_BITS);
1076
1077 if (Mcb) {
1078 rc = Ext2RemoveMcbExtent(Vcb, Mcb, Vbn, Length);
1079 } else {
1080 rc = Ext2RemoveVcbExtent(Vcb, Vbn, Length);
1081 }
1082
1083 return rc;
1084 }
1085
1086 NTSTATUS
1087 Ext2InitializeZone(
1088 IN PEXT2_IRP_CONTEXT IrpContext,
1089 IN PEXT2_VCB Vcb,
1090 IN PEXT2_MCB Mcb
1091 )
1092 {
1093 NTSTATUS Status = STATUS_SUCCESS;
1094
1095 ULONG Start = 0;
1096 ULONG End;
1097 ULONG Block;
1098 ULONG Mapped;
1099
1100 Ext2ClearAllExtents(&Mcb->Extents);
1101 Ext2ClearAllExtents(&Mcb->MetaExts);
1102
1103 ASSERT(Mcb != NULL);
1104 End = (ULONG)((Mcb->Inode.i_size + BLOCK_SIZE - 1) >> BLOCK_BITS);
1105
1106 while (Start < End) {
1107
1108 Block = Mapped = 0;
1109
1110 /* mapping file offset to ext2 block */
1111 if (INODE_HAS_EXTENT(&Mcb->Inode)) {
1112 Status = Ext2MapExtent(
1113 IrpContext,
1114 Vcb,
1115 Mcb,
1116 Start,
1117 FALSE,
1118 &Block,
1119 &Mapped
1120 );
1121 } else {
1122 Status = Ext2MapIndirect(
1123 IrpContext,
1124 Vcb,
1125 Mcb,
1126 Start,
1127 FALSE,
1128 &Block,
1129 &Mapped
1130 );
1131 }
1132
1133 if (!NT_SUCCESS(Status)) {
1134 goto errorout;
1135 }
1136
1137 /* skip wrong blocks, in case wrongly treating symlink
1138 target names as blocks, silly */
1139 if (Block >= TOTAL_BLOCKS) {
1140 Block = 0;
1141 }
1142
1143 if (Block) {
1144 if (!Ext2AddBlockExtent(Vcb, Mcb, Start, Block, Mapped)) {
1145 DbgBreak();
1146 ClearFlag(Mcb->Flags, MCB_ZONE_INITED);
1147 Ext2ClearAllExtents(&Mcb->Extents);
1148 Status = STATUS_INSUFFICIENT_RESOURCES;
1149 goto errorout;
1150 }
1151 DEBUG(DL_MAP, ("Ext2InitializeZone %wZ: Block = %xh Mapped = %xh\n",
1152 &Mcb->FullName, Block, Mapped));
1153 }
1154
1155 /* Mapped is total number of continous blocks or NULL blocks */
1156 Start += Mapped;
1157 }
1158
1159 /* set mcb zone as initialized */
1160 SetLongFlag(Mcb->Flags, MCB_ZONE_INITED);
1161
1162 errorout:
1163
1164 if (!IsZoneInited(Mcb)) {
1165 Ext2ClearAllExtents(&Mcb->Extents);
1166 Ext2ClearAllExtents(&Mcb->MetaExts);
1167 }
1168
1169 return Status;
1170 }
1171
1172 NTSTATUS
1173 Ext2BuildExtents(
1174 IN PEXT2_IRP_CONTEXT IrpContext,
1175 IN PEXT2_VCB Vcb,
1176 IN PEXT2_MCB Mcb,
1177 IN ULONGLONG Offset,
1178 IN ULONG Size,
1179 IN BOOLEAN bAlloc,
1180 OUT PEXT2_EXTENT * Chain
1181 )
1182 {
1183 ULONG Start, End;
1184 ULONG Total = 0;
1185
1186 LONGLONG Lba = 0;
1187 NTSTATUS Status = STATUS_SUCCESS;
1188
1189 PEXT2_EXTENT Extent = NULL;
1190 PEXT2_EXTENT List = *Chain = NULL;
1191
1192 if (!IsZoneInited(Mcb)) {
1193 Status = Ext2InitializeZone(IrpContext, Vcb, Mcb);
1194 if (!NT_SUCCESS(Status)) {
1195 DbgBreak();
1196 }
1197 }
1198
1199 if ((IrpContext && IrpContext->Irp) &&
1200 ((IrpContext->Irp->Flags & IRP_NOCACHE) ||
1201 (IrpContext->Irp->Flags & IRP_PAGING_IO))) {
1202 Size = (Size + SECTOR_SIZE - 1) & (~(SECTOR_SIZE - 1));
1203 }
1204
1205 Start = (ULONG)(Offset >> BLOCK_BITS);
1206 End = (ULONG)((Size + Offset + BLOCK_SIZE - 1) >> BLOCK_BITS);
1207
1208 if (End > (ULONG)((Mcb->Inode.i_size + BLOCK_SIZE - 1) >> BLOCK_BITS) ) {
1209 End = (ULONG)((Mcb->Inode.i_size + BLOCK_SIZE - 1) >> BLOCK_BITS);
1210 }
1211
1212 while (Size > 0 && Start < End) {
1213
1214 ULONG Mapped = 0;
1215 ULONG Length = 0;
1216 ULONG Block = 0;
1217
1218 BOOLEAN rc = FALSE;
1219
1220 /* try to map file offset to ext2 block upon Extents cache */
1221 if (IsZoneInited(Mcb)) {
1222 rc = Ext2LookupBlockExtent(
1223 Vcb,
1224 Mcb,
1225 Start,
1226 &Block,
1227 &Mapped);
1228
1229 if (!rc) {
1230 /* we likely get a sparse file here */
1231 Mapped = 1;
1232 Block = 0;
1233 }
1234 }
1235
1236 /* try to BlockMap in case failed to access Extents cache */
1237 if (!IsZoneInited(Mcb) || (bAlloc && Block == 0)) {
1238
1239 Status = Ext2BlockMap(
1240 IrpContext,
1241 Vcb,
1242 Mcb,
1243 Start,
1244 bAlloc,
1245 &Block,
1246 &Mapped
1247 );
1248 if (!NT_SUCCESS(Status)) {
1249 break;
1250 }
1251
1252 /* skip wrong blocks, in case wrongly treating symlink
1253 target names as blocks, silly */
1254 if (Block >= TOTAL_BLOCKS) {
1255 Block = 0;
1256 }
1257
1258 /* add new allocated blocks to Mcb zone */
1259 if (IsZoneInited(Mcb) && Block) {
1260 if (!Ext2AddBlockExtent(Vcb, Mcb, Start, Block, Mapped)) {
1261 DbgBreak();
1262 ClearFlag(Mcb->Flags, MCB_ZONE_INITED);
1263 Ext2ClearAllExtents(&Mcb->Extents);
1264 }
1265 }
1266 }
1267
1268 /* calculate i/o extent */
1269 Lba = ((LONGLONG)Block << BLOCK_BITS) + Offset - ((LONGLONG)Start << BLOCK_BITS);
1270 Length = (ULONG)(((LONGLONG)(Start + Mapped) << BLOCK_BITS) - Offset);
1271 if (Length > Size) {
1272 Length = Size;
1273 }
1274
1275 if (0 == Length) {
1276 DbgBreak();
1277 break;
1278 }
1279
1280 Start += Mapped;
1281 Offset = (ULONGLONG)Start << BLOCK_BITS;
1282
1283 if (Block != 0) {
1284
1285 if (List && List->Lba + List->Length == Lba) {
1286
1287 /* it's continuous upon previous Extent */
1288 List->Length += Length;
1289
1290 } else {
1291
1292 /* have to allocate a new Extent */
1293 Extent = Ext2AllocateExtent();
1294 if (!Extent) {
1295 Status = STATUS_INSUFFICIENT_RESOURCES;
1296 DbgBreak();
1297 break;
1298 }
1299
1300 Extent->Lba = Lba;
1301 Extent->Length = Length;
1302 Extent->Offset = Total;
1303
1304 /* insert new Extent to chain */
1305 if (List) {
1306 List->Next = Extent;
1307 List = Extent;
1308 } else {
1309 *Chain = List = Extent;
1310 }
1311 }
1312 } else {
1313 if (bAlloc) {
1314 DbgBreak();
1315 }
1316 }
1317
1318 Total += Length;
1319 Size -= Length;
1320 }
1321
1322 return Status;
1323 }
1324
1325
1326 BOOLEAN
1327 Ext2BuildName(
1328 IN OUT PUNICODE_STRING Target,
1329 IN PUNICODE_STRING File,
1330 IN PUNICODE_STRING Parent
1331 )
1332 {
1333 USHORT Length = 0;
1334 USHORT ParentLen = 0;
1335 BOOLEAN bBackslash = TRUE;
1336
1337 /* free the original buffer */
1338 if (Target->Buffer) {
1339 DEC_MEM_COUNT(PS_MCB_NAME, Target->Buffer, Target->MaximumLength);
1340 Ext2FreePool(Target->Buffer, EXT2_FNAME_MAGIC);
1341 Target->Length = Target->MaximumLength = 0;
1342 }
1343
1344 /* check the parent directory's name and backslash */
1345 if (Parent && Parent->Buffer && Parent->Length > 0) {
1346 ParentLen = Parent->Length / sizeof(WCHAR);
1347 if (Parent->Buffer[ParentLen - 1] == L'\\') {
1348 bBackslash = FALSE;
1349 }
1350 }
1351
1352 if (Parent == NULL || File->Buffer[0] == L'\\') {
1353 /* must be root inode */
1354 ASSERT(ParentLen == 0);
1355 bBackslash = FALSE;
1356 }
1357
1358 /* allocate and initialize new name buffer */
1359 Length = File->Length;
1360 Length += (ParentLen + (bBackslash ? 1 : 0)) * sizeof(WCHAR);
1361
1362 Target->Buffer = Ext2AllocatePool(
1363 PagedPool,
1364 Length + 2,
1365 EXT2_FNAME_MAGIC
1366 );
1367
1368 if (!Target->Buffer) {
1369 DEBUG(DL_ERR, ( "Ex2BuildName: failed to allocate name bufer.\n"));
1370 return FALSE;
1371 }
1372 RtlZeroMemory(Target->Buffer, Length + 2);
1373
1374 if (ParentLen) {
1375 RtlCopyMemory(&Target->Buffer[0],
1376 Parent->Buffer,
1377 ParentLen * sizeof(WCHAR));
1378 }
1379
1380 if (bBackslash) {
1381 Target->Buffer[ParentLen++] = L'\\';
1382 }
1383
1384 RtlCopyMemory( &Target->Buffer[ParentLen],
1385 File->Buffer,
1386 File->Length);
1387
1388 INC_MEM_COUNT(PS_MCB_NAME, Target->Buffer, Length + 2);
1389 Target->Length = Length;
1390 Target->MaximumLength = Length + 2;
1391
1392 return TRUE;
1393 }
1394
1395 PEXT2_MCB
1396 Ext2AllocateMcb (
1397 IN PEXT2_VCB Vcb,
1398 IN PUNICODE_STRING FileName,
1399 IN PUNICODE_STRING Parent,
1400 IN ULONG FileAttr
1401 )
1402 {
1403 PEXT2_MCB Mcb = NULL;
1404 NTSTATUS Status = STATUS_SUCCESS;
1405
1406 /* need wake the reaper thread if there are many Mcb allocated */
1407 if (Ext2Global->PerfStat.Current.Mcb > (((ULONG)Ext2Global->MaxDepth) * 4)) {
1408 KeSetEvent(&Ext2Global->McbReaper.Wait, 0, FALSE);
1409 }
1410
1411 /* allocate Mcb from LookasideList */
1412 Mcb = (PEXT2_MCB) (ExAllocateFromNPagedLookasideList(
1413 &(Ext2Global->Ext2McbLookasideList)));
1414
1415 if (Mcb == NULL) {
1416 return NULL;
1417 }
1418
1419 /* initialize Mcb header */
1420 RtlZeroMemory(Mcb, sizeof(EXT2_MCB));
1421 Mcb->Identifier.Type = EXT2MCB;
1422 Mcb->Identifier.Size = sizeof(EXT2_MCB);
1423 Mcb->FileAttr = FileAttr;
1424
1425 Mcb->Inode.i_priv = (PVOID)Mcb;
1426 Mcb->Inode.i_sb = &Vcb->sb;
1427
1428 /* initialize Mcb names */
1429 if (FileName) {
1430
1431 #if EXT2_DEBUG
1432 if ( FileName->Length == 2 &&
1433 FileName->Buffer[0] == L'\\') {
1434 DEBUG(DL_RES, ( "Ext2AllocateMcb: Root Mcb is to be created !\n"));
1435 }
1436
1437 if ( FileName->Length == 2 &&
1438 FileName->Buffer[0] == L'.') {
1439 DbgBreak();
1440 }
1441
1442 if ( FileName->Length == 4 &&
1443 FileName->Buffer[0] == L'.' &&
1444 FileName->Buffer[1] == L'.' ) {
1445 DbgBreak();
1446 }
1447 #endif
1448
1449 if (( FileName->Length >= 4 && FileName->Buffer[0] == L'.') &&
1450 ((FileName->Length == 4 && FileName->Buffer[1] != L'.') ||
1451 FileName->Length >= 6 )) {
1452 SetFlag(Mcb->FileAttr, FILE_ATTRIBUTE_HIDDEN);
1453 }
1454
1455 if (!Ext2BuildName(&Mcb->ShortName, FileName, NULL)) {
1456 goto errorout;
1457 }
1458 if (!Ext2BuildName(&Mcb->FullName, FileName, Parent)) {
1459 goto errorout;
1460 }
1461 }
1462
1463 /* initialize Mcb Extents, it will raise an expcetion if failed */
1464 _SEH2_TRY {
1465 FsRtlInitializeLargeMcb(&(Mcb->Extents), NonPagedPool);
1466 FsRtlInitializeLargeMcb(&(Mcb->MetaExts), NonPagedPool);
1467 } _SEH2_EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
1468 Status = STATUS_INSUFFICIENT_RESOURCES;
1469 DbgBreak();
1470 } _SEH2_END;
1471
1472 if (!NT_SUCCESS(Status)) {
1473 goto errorout;
1474 }
1475
1476 INC_MEM_COUNT(PS_MCB, Mcb, sizeof(EXT2_MCB));
1477 DEBUG(DL_INF, ( "Ext2AllocateMcb: Mcb %wZ created.\n", &Mcb->FullName));
1478
1479 return Mcb;
1480
1481 errorout:
1482
1483 if (Mcb) {
1484
1485 if (Mcb->ShortName.Buffer) {
1486 DEC_MEM_COUNT(PS_MCB_NAME, Mcb->ShortName.Buffer,
1487 Mcb->ShortName.MaximumLength);
1488 Ext2FreePool(Mcb->ShortName.Buffer, EXT2_FNAME_MAGIC);
1489 }
1490
1491 if (Mcb->FullName.Buffer) {
1492 DEC_MEM_COUNT(PS_MCB_NAME, Mcb->FullName.Buffer,
1493 Mcb->FullName.MaximumLength);
1494 Ext2FreePool(Mcb->FullName.Buffer, EXT2_FNAME_MAGIC);
1495 }
1496
1497 ExFreeToNPagedLookasideList(&(Ext2Global->Ext2McbLookasideList), Mcb);
1498 }
1499
1500 return NULL;
1501 }
1502
1503 VOID
1504 Ext2FreeMcb (IN PEXT2_VCB Vcb, IN PEXT2_MCB Mcb)
1505 {
1506 #ifndef __REACTOS__
1507 PEXT2_MCB Parent = Mcb->Parent;
1508 #endif
1509
1510 ASSERT(Mcb != NULL);
1511
1512 ASSERT((Mcb->Identifier.Type == EXT2MCB) &&
1513 (Mcb->Identifier.Size == sizeof(EXT2_MCB)));
1514
1515 if ((Mcb->Identifier.Type != EXT2MCB) ||
1516 (Mcb->Identifier.Size != sizeof(EXT2_MCB))) {
1517 return;
1518 }
1519
1520 DEBUG(DL_INF, ( "Ext2FreeMcb: Mcb %wZ will be freed.\n", &Mcb->FullName));
1521
1522 if (IsMcbSymLink(Mcb) && Mcb->Target) {
1523 Ext2DerefMcb(Mcb->Target);
1524 }
1525
1526 if (FsRtlNumberOfRunsInLargeMcb(&Mcb->Extents)) {
1527 DEBUG(DL_EXT, ("List data extents for: %wZ\n", &Mcb->FullName));
1528 Ext2ListExtents(&Mcb->Extents);
1529 }
1530 FsRtlUninitializeLargeMcb(&(Mcb->Extents));
1531 if (FsRtlNumberOfRunsInLargeMcb(&Mcb->MetaExts)) {
1532 DEBUG(DL_EXT, ("List meta extents for: %wZ\n", &Mcb->FullName));
1533 Ext2ListExtents(&Mcb->MetaExts);
1534 }
1535 FsRtlUninitializeLargeMcb(&(Mcb->MetaExts));
1536 ClearLongFlag(Mcb->Flags, MCB_ZONE_INITED);
1537
1538 if (Mcb->ShortName.Buffer) {
1539 DEC_MEM_COUNT(PS_MCB_NAME, Mcb->ShortName.Buffer,
1540 Mcb->ShortName.MaximumLength);
1541 Ext2FreePool(Mcb->ShortName.Buffer, EXT2_FNAME_MAGIC);
1542 }
1543
1544 if (Mcb->FullName.Buffer) {
1545 DEC_MEM_COUNT(PS_MCB_NAME, Mcb->FullName.Buffer,
1546 Mcb->FullName.MaximumLength);
1547 Ext2FreePool(Mcb->FullName.Buffer, EXT2_FNAME_MAGIC);
1548 }
1549
1550 /* free dentry */
1551 if (Mcb->de) {
1552 Ext2FreeEntry(Mcb->de);
1553 }
1554
1555 Mcb->Identifier.Type = 0;
1556 Mcb->Identifier.Size = 0;
1557
1558 ExFreeToNPagedLookasideList(&(Ext2Global->Ext2McbLookasideList), Mcb);
1559 DEC_MEM_COUNT(PS_MCB, Mcb, sizeof(EXT2_MCB));
1560 }
1561
1562
1563 PEXT2_MCB
1564 Ext2SearchMcb(
1565 PEXT2_VCB Vcb,
1566 PEXT2_MCB Parent,
1567 PUNICODE_STRING FileName
1568 )
1569 {
1570 BOOLEAN LockAcquired = FALSE;
1571 PEXT2_MCB Mcb = NULL;
1572
1573 _SEH2_TRY {
1574 ExAcquireResourceSharedLite(&Vcb->McbLock, TRUE);
1575 LockAcquired = TRUE;
1576 Mcb = Ext2SearchMcbWithoutLock(Parent, FileName);
1577 } _SEH2_FINALLY {
1578 if (LockAcquired) {
1579 ExReleaseResourceLite(&Vcb->McbLock);
1580 }
1581 } _SEH2_END;
1582
1583 return Mcb;
1584 }
1585
1586
1587 PEXT2_MCB
1588 Ext2SearchMcbWithoutLock(
1589 PEXT2_MCB Parent,
1590 PUNICODE_STRING FileName
1591 )
1592 {
1593 PEXT2_MCB TmpMcb = NULL;
1594
1595 DEBUG(DL_RES, ("Ext2SearchMcb: %wZ\n", FileName));
1596
1597 _SEH2_TRY {
1598
1599 Ext2ReferMcb(Parent);
1600
1601 if (Ext2IsDot(FileName)) {
1602 TmpMcb = Parent;
1603 Ext2ReferMcb(Parent);
1604 _SEH2_LEAVE;
1605 }
1606
1607 if (Ext2IsDotDot(FileName)) {
1608 if (IsMcbRoot(Parent)) {
1609 TmpMcb = Parent;
1610 } else {
1611 TmpMcb = Parent->Parent;
1612 }
1613 if (TmpMcb) {
1614 Ext2ReferMcb(TmpMcb);
1615 }
1616 _SEH2_LEAVE;
1617 }
1618
1619 if (IsMcbSymLink(Parent)) {
1620 if (Parent->Target) {
1621 TmpMcb = Parent->Target->Child;
1622 ASSERT(!IsMcbSymLink(Parent->Target));
1623 } else {
1624 TmpMcb = NULL;
1625 _SEH2_LEAVE;
1626 }
1627 } else {
1628 TmpMcb = Parent->Child;
1629 }
1630
1631 while (TmpMcb) {
1632
1633 if (!RtlCompareUnicodeString(
1634 &(TmpMcb->ShortName),
1635 FileName, TRUE )) {
1636 Ext2ReferMcb(TmpMcb);
1637 break;
1638 }
1639
1640 TmpMcb = TmpMcb->Next;
1641 }
1642
1643 } _SEH2_FINALLY {
1644
1645 Ext2DerefMcb(Parent);
1646 } _SEH2_END;
1647
1648 return TmpMcb;
1649 }
1650
1651 VOID
1652 Ext2InsertMcb (
1653 PEXT2_VCB Vcb,
1654 PEXT2_MCB Parent,
1655 PEXT2_MCB Child
1656 )
1657 {
1658 BOOLEAN LockAcquired = FALSE;
1659 PEXT2_MCB Mcb = NULL;
1660
1661 _SEH2_TRY {
1662
1663 ExAcquireResourceExclusiveLite(
1664 &Vcb->McbLock,
1665 TRUE );
1666 LockAcquired = TRUE;
1667
1668 /* use it's target if it's a symlink */
1669 if (IsMcbSymLink(Parent)) {
1670 Parent = Parent->Target;
1671 ASSERT(!IsMcbSymLink(Parent));
1672 }
1673
1674 Mcb = Parent->Child;
1675 while (Mcb) {
1676 if (Mcb == Child) {
1677 break;
1678 }
1679 Mcb = Mcb->Next;
1680 }
1681
1682 if (Mcb) {
1683 /* already attached in the list */
1684 DEBUG(DL_ERR, ( "Ext2InsertMcb: Child Mcb is alreay attached.\n"));
1685 if (!IsFlagOn(Mcb->Flags, MCB_ENTRY_TREE)) {
1686 SetLongFlag(Child->Flags, MCB_ENTRY_TREE);
1687 DEBUG(DL_ERR, ( "Ext2InsertMcb: Child Mcb's flag isn't set.\n"));
1688 }
1689
1690 DbgBreak();
1691
1692 } else {
1693
1694 /* insert this Mcb into the head */
1695 Child->Next = Parent->Child;
1696 Parent->Child = Child;
1697 Child->Parent = Parent;
1698 Child->de->d_parent = Parent->de;
1699 Ext2ReferMcb(Parent);
1700 SetLongFlag(Child->Flags, MCB_ENTRY_TREE);
1701 }
1702
1703 } _SEH2_FINALLY {
1704
1705 if (LockAcquired) {
1706 ExReleaseResourceLite(&Vcb->McbLock);
1707 }
1708 } _SEH2_END;
1709 }
1710
1711 BOOLEAN
1712 Ext2RemoveMcb (
1713 PEXT2_VCB Vcb,
1714 PEXT2_MCB Mcb
1715 )
1716 {
1717 PEXT2_MCB TmpMcb = NULL;
1718 BOOLEAN LockAcquired = FALSE;
1719 BOOLEAN bLinked = FALSE;
1720
1721 _SEH2_TRY {
1722
1723 ExAcquireResourceExclusiveLite(&Vcb->McbLock, TRUE);
1724 LockAcquired = TRUE;
1725
1726 if (Mcb->Parent) {
1727
1728 if (Mcb->Parent->Child == Mcb) {
1729 Mcb->Parent->Child = Mcb->Next;
1730 bLinked = TRUE;
1731 } else {
1732 TmpMcb = Mcb->Parent->Child;
1733
1734 while (TmpMcb && TmpMcb->Next != Mcb) {
1735 TmpMcb = TmpMcb->Next;
1736 }
1737
1738 if (TmpMcb) {
1739 TmpMcb->Next = Mcb->Next;
1740 bLinked = TRUE;
1741 } else {
1742 /* we got errors: link broken */
1743 }
1744 }
1745
1746 if (bLinked) {
1747 if (IsFlagOn(Mcb->Flags, MCB_ENTRY_TREE)) {
1748 DEBUG(DL_RES, ("Mcb %p %wZ removed from Mcb %p %wZ\n", Mcb,
1749 &Mcb->FullName, Mcb->Parent, &Mcb->Parent->FullName));
1750 Ext2DerefMcb(Mcb->Parent);
1751 ClearLongFlag(Mcb->Flags, MCB_ENTRY_TREE);
1752 } else {
1753 DbgBreak();
1754 }
1755 } else {
1756 if (IsFlagOn(Mcb->Flags, MCB_ENTRY_TREE)) {
1757 ClearLongFlag(Mcb->Flags, MCB_ENTRY_TREE);
1758 }
1759 DbgBreak();
1760 }
1761 Mcb->Parent = NULL;
1762 Mcb->de->d_parent = NULL;
1763 }
1764
1765 } _SEH2_FINALLY {
1766
1767 if (LockAcquired) {
1768 ExReleaseResourceLite(&Vcb->McbLock);
1769 }
1770 } _SEH2_END;
1771
1772 return TRUE;
1773 }
1774
1775 VOID
1776 Ext2CleanupAllMcbs(PEXT2_VCB Vcb)
1777 {
1778 BOOLEAN LockAcquired = FALSE;
1779 PEXT2_MCB Mcb = NULL;
1780
1781 _SEH2_TRY {
1782
1783 ExAcquireResourceExclusiveLite(
1784 &Vcb->McbLock,
1785 TRUE );
1786 LockAcquired = TRUE;
1787
1788 while ((Mcb = Ext2FirstUnusedMcb(Vcb, TRUE, Vcb->NumOfMcb)) != 0) {
1789 while (Mcb) {
1790 PEXT2_MCB Next = Mcb->Next;
1791 if (IsMcbSymLink(Mcb)) {
1792 Mcb->Target = NULL;
1793 }
1794 Ext2FreeMcb(Vcb, Mcb);
1795 Mcb = Next;
1796 }
1797 }
1798 Ext2FreeMcb(Vcb, Vcb->McbTree);
1799 Vcb->McbTree = NULL;
1800
1801 } _SEH2_FINALLY {
1802
1803 if (LockAcquired) {
1804 ExReleaseResourceLite(&Vcb->McbLock);
1805 }
1806 } _SEH2_END;
1807 }
1808
1809 BOOLEAN
1810 Ext2CheckSetBlock(PEXT2_IRP_CONTEXT IrpContext, PEXT2_VCB Vcb, LONGLONG Block)
1811 {
1812 PEXT2_GROUP_DESC gd;
1813 struct buffer_head *gb = NULL;
1814 struct buffer_head *bh = NULL;
1815 ULONG group, dwBlk, Length;
1816 RTL_BITMAP bitmap;
1817 BOOLEAN bModified = FALSE;
1818
1819 group = (ULONG)(Block - EXT2_FIRST_DATA_BLOCK) / BLOCKS_PER_GROUP;
1820 dwBlk = (ULONG)(Block - EXT2_FIRST_DATA_BLOCK) % BLOCKS_PER_GROUP;
1821
1822 gd = ext4_get_group_desc(&Vcb->sb, group, &gb);
1823 if (!gd) {
1824 return FALSE;
1825 }
1826 bh = sb_getblk(&Vcb->sb, ext4_block_bitmap(&Vcb->sb, gd));
1827
1828 if (group == Vcb->sbi.s_groups_count - 1) {
1829 Length = (ULONG)(TOTAL_BLOCKS % BLOCKS_PER_GROUP);
1830
1831 /* s_blocks_count is integer multiple of s_blocks_per_group */
1832 if (Length == 0)
1833 Length = BLOCKS_PER_GROUP;
1834 } else {
1835 Length = BLOCKS_PER_GROUP;
1836 }
1837
1838 if (dwBlk >= Length) {
1839 fini_bh(&gb);
1840 fini_bh(&bh);
1841 return FALSE;
1842 }
1843
1844
1845 RtlInitializeBitMap(&bitmap, (PULONG)bh->b_data, Length);
1846
1847 if (RtlCheckBit(&bitmap, dwBlk) == 0) {
1848 DbgBreak();
1849 RtlSetBits(&bitmap, dwBlk, 1);
1850 bModified = TRUE;
1851 mark_buffer_dirty(bh);
1852 }
1853
1854 fini_bh(&gb);
1855 fini_bh(&bh);
1856
1857 return (!bModified);
1858 }
1859
1860 BOOLEAN
1861 Ext2CheckBitmapConsistency(PEXT2_IRP_CONTEXT IrpContext, PEXT2_VCB Vcb)
1862 {
1863 ULONG i, j, InodeBlocks;
1864
1865 for (i = 0; i < Vcb->sbi.s_groups_count; i++) {
1866
1867 PEXT2_GROUP_DESC gd;
1868 struct buffer_head *bh = NULL;
1869
1870 gd = ext4_get_group_desc(&Vcb->sb, i, &bh);
1871 if (!gd)
1872 continue;
1873 Ext2CheckSetBlock(IrpContext, Vcb, ext4_block_bitmap(&Vcb->sb, gd));
1874 Ext2CheckSetBlock(IrpContext, Vcb, ext4_inode_bitmap(&Vcb->sb, gd));
1875
1876
1877 if (i == Vcb->sbi.s_groups_count - 1) {
1878 InodeBlocks = ((INODES_COUNT % INODES_PER_GROUP) *
1879 Vcb->InodeSize + Vcb->BlockSize - 1) /
1880 (Vcb->BlockSize);
1881 } else {
1882 InodeBlocks = (INODES_PER_GROUP * Vcb->InodeSize +
1883 Vcb->BlockSize - 1) / (Vcb->BlockSize);
1884 }
1885
1886 for (j = 0; j < InodeBlocks; j++ )
1887 Ext2CheckSetBlock(IrpContext, Vcb, ext4_inode_table(&Vcb->sb, gd) + j);
1888
1889 fini_bh(&bh);
1890 }
1891
1892 return TRUE;
1893 }
1894
1895 /* Ext2Global->Resource should be already acquired */
1896 VOID
1897 Ext2InsertVcb(PEXT2_VCB Vcb)
1898 {
1899 InsertTailList(&(Ext2Global->VcbList), &Vcb->Next);
1900 }
1901
1902
1903 /* Ext2Global->Resource should be already acquired */
1904 VOID
1905 Ext2RemoveVcb(PEXT2_VCB Vcb)
1906 {
1907 RemoveEntryList(&Vcb->Next);
1908 InitializeListHead(&Vcb->Next);
1909 }
1910
1911 NTSTATUS
1912 Ext2QueryVolumeParams(IN PEXT2_VCB Vcb, IN PUNICODE_STRING Params)
1913 {
1914 NTSTATUS Status;
1915 RTL_QUERY_REGISTRY_TABLE QueryTable[2];
1916
1917 UNICODE_STRING UniName;
1918 PUSHORT UniBuffer = NULL;
1919
1920 USHORT UUID[50];
1921
1922 int i;
1923 int len = 0;
1924
1925 /* zero params */
1926 RtlZeroMemory(Params, sizeof(UNICODE_STRING));
1927
1928 /* constructing volume UUID name */
1929 memset(UUID, 0, sizeof(USHORT) * 50);
1930 for (i=0; i < 16; i++) {
1931 if (i == 0) {
1932 swprintf((wchar_t *)&UUID[len], L"{%2.2X",Vcb->SuperBlock->s_uuid[i]);
1933 len += 3;
1934 } else if (i == 15) {
1935 swprintf((wchar_t *)&UUID[len], L"-%2.2X}", Vcb->SuperBlock->s_uuid[i]);
1936 len +=4;
1937 } else {
1938 swprintf((wchar_t *)&UUID[len], L"-%2.2X", Vcb->SuperBlock->s_uuid[i]);
1939 len += 3;
1940 }
1941 }
1942
1943 /* allocating memory for UniBuffer */
1944 UniBuffer = Ext2AllocatePool(PagedPool, 1024, EXT2_PARAM_MAGIC);
1945 if (NULL == UniBuffer) {
1946 Status = STATUS_INSUFFICIENT_RESOURCES;
1947 goto errorout;
1948 }
1949 RtlZeroMemory(UniBuffer, 1024);
1950
1951 /* querying volume parameter string */
1952 RtlZeroMemory(&QueryTable[0], sizeof(RTL_QUERY_REGISTRY_TABLE) * 2);
1953 QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT | RTL_QUERY_REGISTRY_REQUIRED;
1954 QueryTable[0].Name = UUID;
1955 QueryTable[0].EntryContext = &(UniName);
1956 UniName.MaximumLength = 1024;
1957 UniName.Length = 0;
1958 UniName.Buffer = UniBuffer;
1959
1960 Status = RtlQueryRegistryValues(
1961 RTL_REGISTRY_ABSOLUTE,
1962 Ext2Global->RegistryPath.Buffer,
1963 &QueryTable[0],
1964 NULL,
1965 NULL
1966 );
1967
1968 if (!NT_SUCCESS(Status)) {
1969 goto errorout;
1970 }
1971
1972 errorout:
1973
1974 if (NT_SUCCESS(Status)) {
1975 *Params = UniName;
1976 } else {
1977 if (UniBuffer) {
1978 Ext2FreePool(UniBuffer, EXT2_PARAM_MAGIC);
1979 }
1980 }
1981
1982 return Status;
1983 }
1984
1985 VOID
1986 Ext2ParseRegistryVolumeParams(
1987 IN PUNICODE_STRING Params,
1988 OUT PEXT2_VOLUME_PROPERTY3 Property
1989 )
1990 {
1991 WCHAR Codepage[CODEPAGE_MAXLEN];
1992 WCHAR Prefix[HIDINGPAT_LEN];
1993 WCHAR Suffix[HIDINGPAT_LEN];
1994 USHORT MountPoint[4];
1995 UCHAR DrvLetter[4];
1996 WCHAR wUID[8], wGID[8], wEUID[8], wEGID[8];
1997 CHAR sUID[8], sGID[8], sEUID[8], sEGID[8];
1998
1999 BOOLEAN bWriteSupport = FALSE,
2000 bCheckBitmap = FALSE,
2001 bCodeName = FALSE,
2002 bMountPoint = FALSE;
2003 BOOLEAN bUID = 0, bGID = 0, bEUID = 0, bEGID = 0;
2004
2005 struct {
2006 PWCHAR Name; /* parameters name */
2007 PBOOLEAN bExist; /* is it contained in params */
2008 USHORT Length; /* parameter value length */
2009 PWCHAR uValue; /* value buffer in unicode */
2010 PCHAR aValue; /* value buffer in ansi */
2011 } ParamPattern[] = {
2012 /* writing support */
2013 {READING_ONLY, &Property->bReadonly, 0, NULL, NULL},
2014 {WRITING_SUPPORT, &bWriteSupport, 0, NULL, NULL},
2015 {EXT3_FORCEWRITING, &Property->bExt3Writable, 0, NULL, NULL},
2016
2017 /* need check bitmap */
2018 {CHECKING_BITMAP, &bCheckBitmap, 0, NULL, NULL},
2019 /* codepage */
2020 {CODEPAGE_NAME, &bCodeName, CODEPAGE_MAXLEN,
2021 &Codepage[0], Property->Codepage},
2022 /* filter prefix and suffix */
2023 {HIDING_PREFIX, &Property->bHidingPrefix, HIDINGPAT_LEN,
2024 &Prefix[0], Property->sHidingPrefix},
2025 {HIDING_SUFFIX, &Property->bHidingSuffix, HIDINGPAT_LEN,
2026 &Suffix[0], Property->sHidingSuffix},
2027 {MOUNT_POINT, &bMountPoint, 4,
2028 &MountPoint[0], &DrvLetter[0]},
2029
2030 {UID, &bUID, 8, &wUID[0], &sUID[0],},
2031 {GID, &bGID, 8, &wGID[0], &sGID[0]},
2032 {EUID, &bEUID, 8, &wEUID[0], &sEUID[0]},
2033 {EGID, &bEGID, 8, &wEGID[0], &sEGID[0]},
2034
2035 /* end */
2036 {NULL, NULL, 0, NULL}
2037 };
2038
2039 USHORT i, j, k;
2040
2041 RtlZeroMemory(Codepage, CODEPAGE_MAXLEN);
2042 RtlZeroMemory(Prefix, HIDINGPAT_LEN);
2043 RtlZeroMemory(Suffix, HIDINGPAT_LEN);
2044 RtlZeroMemory(MountPoint, sizeof(USHORT) * 4);
2045 RtlZeroMemory(DrvLetter, sizeof(CHAR) * 4);
2046
2047 RtlZeroMemory(Property, sizeof(EXT2_VOLUME_PROPERTY3));
2048 Property->Magic = EXT2_VOLUME_PROPERTY_MAGIC;
2049 Property->Command = APP_CMD_SET_PROPERTY3;
2050
2051 for (i=0; ParamPattern[i].Name != NULL; i++) {
2052
2053 UNICODE_STRING Name1=*Params, Name2;
2054 RtlInitUnicodeString(&Name2, ParamPattern[i].Name);
2055 *ParamPattern[i].bExist = FALSE;
2056
2057 for (j=0; j * sizeof(WCHAR) + Name2.Length <= Params->Length ; j++) {
2058
2059 Name1.MaximumLength = Params->Length - j * sizeof(WCHAR);
2060 Name1.Length = Name2.Length;
2061 Name1.Buffer = &Params->Buffer[j];
2062
2063 if (!RtlCompareUnicodeString(&Name1, &Name2, TRUE)) {
2064 if (j * sizeof(WCHAR) + Name2.Length == Params->Length ||
2065 Name1.Buffer[Name2.Length/sizeof(WCHAR)] == L';' ||
2066 Name1.Buffer[Name2.Length/sizeof(WCHAR)] == L',' ) {
2067 *(ParamPattern[i].bExist) = TRUE;
2068 } else if ((j * 2 + Name2.Length < Params->Length + 2) ||
2069 (Name1.Buffer[Name2.Length/sizeof(WCHAR)] == L'=' )) {
2070 j += Name2.Length/sizeof(WCHAR) + 1;
2071 k = 0;
2072 while ( j + k < Params->Length/2 &&
2073 k < ParamPattern[i].Length &&
2074 Params->Buffer[j+k] != L';' &&
2075 Params->Buffer[j+k] != L',' ) {
2076 #ifdef __REACTOS__
2077 ParamPattern[i].uValue[k] = Params->Buffer[j + k];
2078 k++;
2079 #else
2080 ParamPattern[i].uValue[k] = Params->Buffer[j + k++];
2081 #endif
2082 }
2083 if (k) {
2084 NTSTATUS status;
2085 ANSI_STRING AnsiName;
2086 AnsiName.Length = 0;
2087 AnsiName.MaximumLength =ParamPattern[i].Length;
2088 AnsiName.Buffer = ParamPattern[i].aValue;
2089
2090 Name2.Buffer = ParamPattern[i].uValue;
2091 Name2.MaximumLength = Name2.Length = k * sizeof(WCHAR);
2092 status = RtlUnicodeStringToAnsiString(
2093 &AnsiName, &Name2, FALSE);
2094 if (NT_SUCCESS(status)) {
2095 *(ParamPattern[i].bExist) = TRUE;
2096 } else {
2097 *ParamPattern[i].bExist = FALSE;
2098 }
2099 }
2100 }
2101 break;
2102 }
2103 }
2104 }
2105
2106 if (bMountPoint) {
2107 Property->DrvLetter = DrvLetter[0];
2108 Property->DrvLetter |= 0x80;
2109 }
2110
2111 if (bUID && bGID) {
2112 SetFlag(Property->Flags2, EXT2_VPROP3_USERIDS);
2113 sUID[7] = sGID[7] = sEUID[7] = sEGID[7] = 0;
2114 Property->uid = (USHORT)atoi(sUID);
2115 Property->gid = (USHORT)atoi(sGID);
2116 if (bEUID) {
2117 Property->euid = (USHORT)atoi(sEUID);
2118 Property->egid = (USHORT)atoi(sEGID);
2119 Property->EIDS = TRUE;
2120 } else {
2121 Property->EIDS = FALSE;
2122 }
2123 } else {
2124 ClearFlag(Property->Flags2, EXT2_VPROP3_USERIDS);
2125 }
2126 }
2127
2128 NTSTATUS
2129 Ext2PerformRegistryVolumeParams(IN PEXT2_VCB Vcb)
2130 {
2131 NTSTATUS Status;
2132 UNICODE_STRING VolumeParams;
2133
2134 Status = Ext2QueryVolumeParams(Vcb, &VolumeParams);
2135 if (NT_SUCCESS(Status)) {
2136
2137 /* set Vcb settings from registery */
2138 EXT2_VOLUME_PROPERTY3 Property;
2139 Ext2ParseRegistryVolumeParams(&VolumeParams, &Property);
2140 Ext2ProcessVolumeProperty(Vcb, &Property, sizeof(Property));
2141
2142 } else {
2143
2144 /* don't support auto mount */
2145 if (IsFlagOn(Ext2Global->Flags, EXT2_AUTO_MOUNT)) {
2146 Status = STATUS_SUCCESS;
2147 } else {
2148 Status = STATUS_UNSUCCESSFUL;
2149 goto errorout;
2150 }
2151
2152 /* set Vcb settings from Ext2Global */
2153 if (IsFlagOn(Ext2Global->Flags, EXT2_SUPPORT_WRITING)) {
2154 if (Vcb->IsExt3fs) {
2155 if (IsFlagOn(Ext2Global->Flags, EXT3_FORCE_WRITING)) {
2156 ClearLongFlag(Vcb->Flags, VCB_READ_ONLY);
2157 } else {
2158 SetLongFlag(Vcb->Flags, VCB_READ_ONLY);
2159 }
2160 } else {
2161 ClearLongFlag(Vcb->Flags, VCB_READ_ONLY);
2162 }
2163 } else {
2164 SetLongFlag(Vcb->Flags, VCB_READ_ONLY);
2165 }
2166
2167 /* set the default codepage */
2168 Vcb->Codepage.PageTable = Ext2Global->Codepage.PageTable;
2169 memcpy(Vcb->Codepage.AnsiName, Ext2Global->Codepage.AnsiName, CODEPAGE_MAXLEN);
2170 Vcb->Codepage.PageTable = Ext2Global->Codepage.PageTable;
2171
2172 if (Vcb->bHidingPrefix == Ext2Global->bHidingPrefix) {
2173 RtlCopyMemory( Vcb->sHidingPrefix,
2174 Ext2Global->sHidingPrefix,
2175 HIDINGPAT_LEN);
2176 } else {
2177 RtlZeroMemory( Vcb->sHidingPrefix,
2178 HIDINGPAT_LEN);
2179 }
2180
2181 if (Vcb->bHidingSuffix == Ext2Global->bHidingSuffix) {
2182 RtlCopyMemory( Vcb->sHidingSuffix,
2183 Ext2Global->sHidingSuffix,
2184 HIDINGPAT_LEN);
2185 } else {
2186 RtlZeroMemory( Vcb->sHidingSuffix,
2187 HIDINGPAT_LEN);
2188 }
2189 }
2190
2191 errorout:
2192
2193 if (VolumeParams.Buffer) {
2194 Ext2FreePool(VolumeParams.Buffer, EXT2_PARAM_MAGIC);
2195 }
2196
2197 return Status;
2198 }
2199
2200 NTSTATUS
2201 Ext2InitializeLabel(
2202 IN PEXT2_VCB Vcb,
2203 IN PEXT2_SUPER_BLOCK Sb
2204 )
2205 {
2206 NTSTATUS status;
2207
2208 USHORT Length;
2209 UNICODE_STRING Label;
2210 OEM_STRING OemName;
2211
2212 Label.MaximumLength = 16 * sizeof(WCHAR);
2213 Label.Length = 0;
2214 Label.Buffer = Vcb->Vpb->VolumeLabel;
2215 Vcb->Vpb->VolumeLabelLength = 0;
2216 RtlZeroMemory(Label.Buffer, Label.MaximumLength);
2217
2218 Length = 16;
2219 while ( (Length > 0) &&
2220 ((Sb->s_volume_name[Length -1] == 0x00) ||
2221 (Sb->s_volume_name[Length - 1] == 0x20) )
2222 ) {
2223 Length--;
2224 }
2225
2226 if (Length == 0) {
2227 return STATUS_SUCCESS;
2228 }
2229
2230 OemName.Buffer = Sb->s_volume_name;
2231 OemName.MaximumLength = 16;
2232 OemName.Length = Length;
2233
2234 status = Ext2OEMToUnicode(Vcb, &Label, &OemName);
2235 if (NT_SUCCESS(status)) {
2236 Vcb->Vpb->VolumeLabelLength = Label.Length;
2237 }
2238
2239 return status;
2240 }
2241
2242 static __inline BOOLEAN Ext2IsNullUuid (__u8 * uuid)
2243 {
2244 int i;
2245 for (i = 0; i < 16; i++) {
2246 if (uuid[i]) {
2247 break;
2248 }
2249 }
2250
2251 return (i >= 16);
2252 }
2253
2254 #define is_power_of_2(x) ((x) != 0 && (((x) & ((x) - 1)) == 0))
2255
2256 NTSTATUS
2257 Ext2InitializeVcb( IN PEXT2_IRP_CONTEXT IrpContext,
2258 IN PEXT2_VCB Vcb,
2259 IN PEXT2_SUPER_BLOCK sb,
2260 IN PDEVICE_OBJECT TargetDevice,
2261 IN PDEVICE_OBJECT VolumeDevice,
2262 IN PVPB Vpb )
2263 {
2264 NTSTATUS Status = STATUS_UNRECOGNIZED_VOLUME;
2265 ULONG IoctlSize;
2266 LONGLONG DiskSize;
2267 LONGLONG PartSize;
2268 UNICODE_STRING RootNode;
2269 USHORT Buffer[2];
2270 ULONG ChangeCount = 0, features;
2271 CC_FILE_SIZES FileSizes;
2272 int i, has_huge_files;
2273
2274 BOOLEAN VcbResourceInitialized = FALSE;
2275 BOOLEAN NotifySyncInitialized = FALSE;
2276 BOOLEAN ExtentsInitialized = FALSE;
2277 BOOLEAN InodeLookasideInitialized = FALSE;
2278 BOOLEAN GroupLoaded = FALSE;
2279
2280 _SEH2_TRY {
2281
2282 if (Vpb == NULL) {
2283 Status = STATUS_DEVICE_NOT_READY;
2284 _SEH2_LEAVE;
2285 }
2286
2287 /* checking in/compat features */
2288 if (IsFlagOn(sb->s_feature_compat, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2289 Vcb->IsExt3fs = TRUE;
2290 }
2291
2292 /* don't mount any volumes with external journal devices */
2293 if (IsFlagOn(sb->s_feature_incompat, EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2294 Status = STATUS_UNRECOGNIZED_VOLUME;
2295 _SEH2_LEAVE;
2296 }
2297
2298 /* check block size */
2299 Vcb->BlockSize = (EXT2_MIN_BLOCK_SIZE << sb->s_log_block_size);
2300 /* we cannot handle volume with block size bigger than 64k */
2301 if (Vcb->BlockSize > EXT2_MAX_USER_BLKSIZE) {
2302 Status = STATUS_UNRECOGNIZED_VOLUME;
2303 _SEH2_LEAVE;
2304 }
2305
2306 if (Vcb->BlockSize >= PAGE_SIZE) {
2307 Vcb->IoUnitBits = PAGE_SHIFT;
2308 Vcb->IoUnitSize = PAGE_SIZE;
2309 } else {
2310 Vcb->IoUnitSize = Vcb->BlockSize;
2311 Vcb->IoUnitBits = Ext2Log2(Vcb->BlockSize);
2312 }
2313
2314 /* initialize vcb header members ... */
2315 Vcb->Header.IsFastIoPossible = FastIoIsNotPossible;
2316 Vcb->Header.Resource = &(Vcb->MainResource);
2317 Vcb->Header.PagingIoResource = &(Vcb->PagingIoResource);
2318 Vcb->OpenVolumeCount = 0;
2319 Vcb->OpenHandleCount = 0;
2320 Vcb->ReferenceCount = 0;
2321
2322 /* initialize eresources */
2323 ExInitializeResourceLite(&Vcb->MainResource);
2324 ExInitializeResourceLite(&Vcb->PagingIoResource);
2325 ExInitializeResourceLite(&Vcb->MetaInode);
2326 ExInitializeResourceLite(&Vcb->MetaBlock);
2327 ExInitializeResourceLite(&Vcb->McbLock);
2328 ExInitializeResourceLite(&Vcb->sbi.s_gd_lock);
2329 #ifndef _WIN2K_TARGET_
2330 ExInitializeFastMutex(&Vcb->Mutex);
2331 FsRtlSetupAdvancedHeader(&Vcb->Header, &Vcb->Mutex);
2332 #endif
2333 VcbResourceInitialized = TRUE;
2334
2335 /* initialize Fcb list head */
2336 InitializeListHead(&Vcb->FcbList);
2337 KeInitializeSpinLock(&Vcb->FcbLock);
2338
2339 /* initialize Mcb list head */
2340 InitializeListHead(&(Vcb->McbList));
2341
2342 /* initialize directory notify list */
2343 InitializeListHead(&Vcb->NotifyList);
2344 FsRtlNotifyInitializeSync(&Vcb->NotifySync);
2345 NotifySyncInitialized = TRUE;
2346
2347 /* superblock checking */
2348 Vcb->SuperBlock = sb;
2349
2350 /* initialize Vpb and Label */
2351 Vcb->DeviceObject = VolumeDevice;
2352 Vcb->TargetDeviceObject = TargetDevice;
2353 Vcb->Vpb = Vpb;
2354 Vcb->RealDevice = Vpb->RealDevice;
2355 Vpb->DeviceObject = VolumeDevice;
2356
2357 /* set inode size */
2358 Vcb->InodeSize = (ULONG)sb->s_inode_size;
2359 if (Vcb->InodeSize == 0) {
2360 Vcb->InodeSize = EXT2_GOOD_OLD_INODE_SIZE;
2361 }
2362
2363 /* initialize inode lookaside list */
2364 ExInitializeNPagedLookasideList(&(Vcb->InodeLookasideList),
2365 NULL, NULL, 0, sizeof(EXT2_INODE),
2366 'SNIE', 0);
2367
2368 InodeLookasideInitialized = TRUE;
2369
2370 /* initialize label in Vpb */
2371 Status = Ext2InitializeLabel(Vcb, sb);
2372 if (!NT_SUCCESS(Status)) {
2373 DbgBreak();
2374 }
2375
2376 /* check device characteristics flags */
2377 if (IsFlagOn(Vpb->RealDevice->Characteristics, FILE_REMOVABLE_MEDIA)) {
2378 SetLongFlag(Vcb->Flags, VCB_REMOVABLE_MEDIA);
2379 }
2380
2381 if (IsFlagOn(Vpb->RealDevice->Characteristics, FILE_FLOPPY_DISKETTE)) {
2382 SetLongFlag(Vcb->Flags, VCB_FLOPPY_DISK);
2383 }
2384
2385 if (IsFlagOn(Vpb->RealDevice->Characteristics, FILE_READ_ONLY_DEVICE)) {
2386 SetLongFlag(Vcb->Flags, VCB_WRITE_PROTECTED);
2387 }
2388
2389 if (IsFlagOn(TargetDevice->Characteristics, FILE_READ_ONLY_DEVICE)) {
2390 SetLongFlag(Vcb->Flags, VCB_WRITE_PROTECTED);
2391 }
2392
2393 /* verify device is writable ? */
2394 if (Ext2IsMediaWriteProtected(IrpContext, TargetDevice)) {
2395 SetFlag(Vcb->Flags, VCB_WRITE_PROTECTED);
2396 }
2397
2398 /* initialize UUID and serial number */
2399 if (Ext2IsNullUuid(sb->s_uuid)) {
2400 ExUuidCreate((UUID *)sb->s_uuid);
2401 }
2402 Vpb->SerialNumber = ((ULONG*)sb->s_uuid)[0] +
2403 ((ULONG*)sb->s_uuid)[1] +
2404 ((ULONG*)sb->s_uuid)[2] +
2405 ((ULONG*)sb->s_uuid)[3];
2406
2407 /* query partition size and disk geometry parameters */
2408 DiskSize = Vcb->DiskGeometry.Cylinders.QuadPart *
2409 Vcb->DiskGeometry.TracksPerCylinder *
2410 Vcb->DiskGeometry.SectorsPerTrack *
2411 Vcb->DiskGeometry.BytesPerSector;
2412
2413 IoctlSize = sizeof(PARTITION_INFORMATION);
2414 Status = Ext2DiskIoControl(
2415 TargetDevice,
2416 IOCTL_DISK_GET_PARTITION_INFO,
2417 NULL,
2418 0,
2419 &Vcb->PartitionInformation,
2420 &IoctlSize );
2421 if (NT_SUCCESS(Status)) {
2422 PartSize = Vcb->PartitionInformation.PartitionLength.QuadPart;
2423 } else {
2424 Vcb->PartitionInformation.StartingOffset.QuadPart = 0;
2425 Vcb->PartitionInformation.PartitionLength.QuadPart = DiskSize;
2426 PartSize = DiskSize;
2427 Status = STATUS_SUCCESS;
2428 }
2429 Vcb->Header.AllocationSize.QuadPart =
2430 Vcb->Header.FileSize.QuadPart = PartSize;
2431
2432 Vcb->Header.ValidDataLength.QuadPart =
2433 Vcb->Header.FileSize.QuadPart;
2434
2435 /* verify count */
2436 IoctlSize = sizeof(ULONG);
2437 Status = Ext2DiskIoControl(
2438 TargetDevice,
2439 IOCTL_DISK_CHECK_VERIFY,
2440 NULL,
2441 0,
2442 &ChangeCount,
2443 &IoctlSize );
2444
2445 if (!NT_SUCCESS(Status)) {
2446 _SEH2_LEAVE;
2447 }
2448 Vcb->ChangeCount = ChangeCount;
2449
2450 /* create the stream object for ext2 volume */
2451 Vcb->Volume = IoCreateStreamFileObject(NULL, Vcb->Vpb->RealDevice);
2452 if (!Vcb->Volume) {
2453 Status = STATUS_UNRECOGNIZED_VOLUME;
2454 _SEH2_LEAVE;
2455 }
2456
2457 /* initialize streaming object file */
2458 Vcb->Volume->SectionObjectPointer = &(Vcb->SectionObject);
2459 Vcb->Volume->ReadAccess = TRUE;
2460 Vcb->Volume->WriteAccess = TRUE;
2461 Vcb->Volume->DeleteAccess = TRUE;
2462 Vcb->Volume->FsContext = (PVOID) Vcb;
2463 Vcb->Volume->FsContext2 = NULL;
2464 Vcb->Volume->Vpb = Vcb->Vpb;
2465
2466 FileSizes.AllocationSize.QuadPart =
2467 FileSizes.FileSize.QuadPart =
2468 FileSizes.ValidDataLength.QuadPart =
2469 Vcb->Header.AllocationSize.QuadPart;
2470
2471 CcInitializeCacheMap( Vcb->Volume,
2472 &FileSizes,
2473 TRUE,
2474 &(Ext2Global->CacheManagerNoOpCallbacks),
2475 Vcb );
2476
2477 /* initialize disk block LargetMcb and entry Mcb,
2478 it will raise an expcetion if failed */
2479 _SEH2_TRY {
2480 FsRtlInitializeLargeMcb(&(Vcb->Extents), PagedPool);
2481 } _SEH2_EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
2482 Status = STATUS_INSUFFICIENT_RESOURCES;
2483 DbgBreak();
2484 } _SEH2_END;
2485 if (!NT_SUCCESS(Status)) {
2486 _SEH2_LEAVE;
2487 }
2488 ExtentsInitialized = TRUE;
2489
2490 /* set block device */
2491 Vcb->bd.bd_dev = Vcb->RealDevice;
2492 Vcb->bd.bd_geo = Vcb->DiskGeometry;
2493 Vcb->bd.bd_part = Vcb->PartitionInformation;
2494 Vcb->bd.bd_volume = Vcb->Volume;
2495 Vcb->bd.bd_priv = (void *) Vcb;
2496 memset(&Vcb->bd.bd_bh_root, 0, sizeof(struct rb_root));
2497 InitializeListHead(&Vcb->bd.bd_bh_free);
2498 ExInitializeResourceLite(&Vcb->bd.bd_bh_lock);
2499 KeInitializeEvent(&Vcb->bd.bd_bh_notify,
2500 NotificationEvent, TRUE);
2501 Vcb->bd.bd_bh_cache = kmem_cache_create("bd_bh_buffer",
2502 Vcb->BlockSize, 0, 0, NULL);
2503 if (!Vcb->bd.bd_bh_cache) {
2504 Status = STATUS_INSUFFICIENT_RESOURCES;
2505 _SEH2_LEAVE;
2506 }
2507
2508 Vcb->SectorBits = Ext2Log2(SECTOR_SIZE);
2509 Vcb->sb.s_magic = sb->s_magic;
2510 Vcb->sb.s_bdev = &Vcb->bd;
2511 Vcb->sb.s_blocksize = BLOCK_SIZE;
2512 Vcb->sb.s_blocksize_bits = BLOCK_BITS;
2513 Vcb->sb.s_priv = (void *) Vcb;
2514 Vcb->sb.s_fs_info = &Vcb->sbi;
2515
2516 Vcb->sbi.s_es = sb;
2517 Vcb->sbi.s_blocks_per_group = sb->s_blocks_per_group;
2518 Vcb->sbi.s_first_ino = sb->s_first_ino;
2519 Vcb->sbi.s_desc_size = sb->s_desc_size;
2520
2521 if (EXT3_HAS_INCOMPAT_FEATURE(&Vcb->sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
2522 if (Vcb->sbi.s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
2523 Vcb->sbi.s_desc_size > EXT4_MAX_DESC_SIZE ||
2524 !is_power_of_2(Vcb->sbi.s_desc_size)) {
2525 DEBUG(DL_ERR, ("EXT4-fs: unsupported descriptor size %lu\n", Vcb->sbi.s_desc_size));
2526 Status = STATUS_DISK_CORRUPT_ERROR;
2527 _SEH2_LEAVE;
2528 }
2529 } else {
2530 Vcb->sbi.s_desc_size = EXT4_MIN_DESC_SIZE;
2531 }
2532
2533 Vcb->sbi.s_blocks_per_group = sb->s_blocks_per_group;
2534 Vcb->sbi.s_inodes_per_group = sb->s_inodes_per_group;
2535 if (EXT3_INODES_PER_GROUP(&Vcb->sb) == 0) {
2536 Status = STATUS_DISK_CORRUPT_ERROR;
2537 _SEH2_LEAVE;
2538 }
2539 Vcb->sbi.s_inodes_per_block = BLOCK_SIZE / Vcb->InodeSize;
2540 if (Vcb->sbi.s_inodes_per_block == 0) {
2541 Status = STATUS_DISK_CORRUPT_ERROR;
2542 _SEH2_LEAVE;
2543 }
2544 Vcb->sbi.s_itb_per_group = Vcb->sbi.s_inodes_per_group /
2545 Vcb->sbi.s_inodes_per_block;
2546
2547
2548 Vcb->sbi.s_desc_per_block = BLOCK_SIZE / GROUP_DESC_SIZE;
2549 Vcb->sbi.s_desc_per_block_bits = ilog2(Vcb->sbi.s_desc_per_block);
2550
2551 for (i=0; i < 4; i++) {
2552 Vcb->sbi.s_hash_seed[i] = sb->s_hash_seed[i];
2553 }
2554 Vcb->sbi.s_def_hash_version = sb->s_def_hash_version;
2555
2556 if (le32_to_cpu(sb->s_rev_level) == EXT3_GOOD_OLD_REV &&
2557 (EXT3_HAS_COMPAT_FEATURE(&Vcb->sb, ~0U) ||
2558 EXT3_HAS_RO_COMPAT_FEATURE(&Vcb->sb, ~0U) ||
2559 EXT3_HAS_INCOMPAT_FEATURE(&Vcb->sb, ~0U))) {
2560 printk(KERN_WARNING
2561 "EXT3-fs warning: feature flags set on rev 0 fs, "
2562 "running e2fsck is recommended\n");
2563 }
2564
2565 /*
2566 * Check feature flags regardless of the revision level, since we
2567 * previously didn't change the revision level when setting the flags,
2568 * so there is a chance incompat flags are set on a rev 0 filesystem.
2569 */
2570 features = EXT3_HAS_INCOMPAT_FEATURE(&Vcb->sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
2571 if (features & EXT4_FEATURE_INCOMPAT_DIRDATA) {
2572 SetLongFlag(Vcb->Flags, VCB_READ_ONLY);
2573 ClearFlag(features, EXT4_FEATURE_INCOMPAT_DIRDATA);
2574 }
2575 if (features) {
2576 printk(KERN_ERR "EXT3-fs: %s: couldn't mount because of "
2577 "unsupported optional features (%x).\n",
2578 Vcb->sb.s_id, le32_to_cpu(features));
2579 Status = STATUS_UNRECOGNIZED_VOLUME;
2580 _SEH2_LEAVE;
2581 }
2582
2583 features = EXT3_HAS_RO_COMPAT_FEATURE(&Vcb->sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
2584 if (features) {
2585 printk(KERN_ERR "EXT3-fs: %s: unsupported optional features in this volume: (%x).\n",
2586 Vcb->sb.s_id, le32_to_cpu(features));
2587 if (CanIWrite(Vcb)) {
2588 } else {
2589 SetLongFlag(Vcb->Flags, VCB_READ_ONLY);
2590 }
2591 }
2592
2593 has_huge_files = EXT3_HAS_RO_COMPAT_FEATURE(&Vcb->sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2594
2595 Vcb->sb.s_maxbytes = ext3_max_size(BLOCK_BITS, has_huge_files);
2596 Vcb->max_bitmap_bytes = ext3_max_bitmap_size(BLOCK_BITS,
2597 has_huge_files);
2598 Vcb->max_bytes = ext3_max_size(BLOCK_BITS, has_huge_files);
2599
2600 /* calculate maximum file bocks ... */
2601 {
2602 ULONG dwData[EXT2_BLOCK_TYPES] = {EXT2_NDIR_BLOCKS, 1, 1, 1};
2603 ULONG i;
2604
2605 ASSERT(BLOCK_BITS == Ext2Log2(BLOCK_SIZE));
2606
2607 Vcb->sbi.s_groups_count = (ULONG)(ext3_blocks_count(sb) - sb->s_first_data_block +
2608 sb->s_blocks_per_group - 1) / sb->s_blocks_per_group;
2609
2610 Vcb->max_data_blocks = 0;
2611 for (i = 0; i < EXT2_BLOCK_TYPES; i++) {
2612 if (BLOCK_BITS >= 12 && i == (EXT2_BLOCK_TYPES - 1)) {
2613 dwData[i] = 0x40000000;
2614 } else {
2615 dwData[i] = dwData[i] << ((BLOCK_BITS - 2) * i);
2616 }
2617 Vcb->max_blocks_per_layer[i] = dwData[i];
2618 Vcb->max_data_blocks += Vcb->max_blocks_per_layer[i];
2619 }
2620 }
2621
2622 Vcb->sbi.s_gdb_count = (Vcb->sbi.s_groups_count + Vcb->sbi.s_desc_per_block - 1) /
2623 Vcb->sbi.s_desc_per_block;
2624 /* load all gorup desc */
2625 if (!Ext2LoadGroup(Vcb)) {
2626 Status = STATUS_UNSUCCESSFUL;
2627 _SEH2_LEAVE;
2628 }
2629 GroupLoaded = TRUE;
2630
2631 /* recovery journal since it's ext3 */
2632 if (Vcb->IsExt3fs) {
2633 Ext2RecoverJournal(IrpContext, Vcb);
2634 if (IsFlagOn(Vcb->Flags, VCB_JOURNAL_RECOVER)) {
2635 SetLongFlag(Vcb->Flags, VCB_READ_ONLY);
2636 }
2637 }
2638
2639 /* Now allocating the mcb for root ... */
2640 Buffer[0] = L'\\';
2641 Buffer[1] = 0;
2642 RootNode.Buffer = Buffer;
2643 RootNode.MaximumLength = RootNode.Length = 2;
2644 Vcb->McbTree = Ext2AllocateMcb(
2645 Vcb, &RootNode, NULL,
2646 FILE_ATTRIBUTE_DIRECTORY
2647 );
2648 if (!Vcb->McbTree) {
2649 DbgBreak();
2650 Status = STATUS_UNSUCCESSFUL;
2651 _SEH2_LEAVE;
2652 }
2653
2654 Vcb->sb.s_root = Ext2BuildEntry(Vcb, NULL, &RootNode);
2655 if (!Vcb->sb.s_root) {
2656 DbgBreak();
2657 Status = STATUS_UNSUCCESSFUL;
2658 _SEH2_LEAVE;
2659 }
2660 Vcb->sb.s_root->d_sb = &Vcb->sb;
2661 Vcb->sb.s_root->d_inode = &Vcb->McbTree->Inode;
2662 Vcb->McbTree->de = Vcb->sb.s_root;
2663
2664 /* load root inode */
2665 Vcb->McbTree->Inode.i_ino = EXT2_ROOT_INO;
2666 Vcb->McbTree->Inode.i_sb = &Vcb->sb;
2667 if (!Ext2LoadInode(Vcb, &Vcb->McbTree->Inode)) {
2668 DbgBreak();
2669 Status = STATUS_CANT_WAIT;
2670 _SEH2_LEAVE;
2671 }
2672
2673 /* initializeroot node */
2674 Vcb->McbTree->CreationTime = Ext2NtTime(Vcb->McbTree->Inode.i_ctime);
2675 Vcb->McbTree->LastAccessTime = Ext2NtTime(Vcb->McbTree->Inode.i_atime);
2676 Vcb->McbTree->LastWriteTime = Ext2NtTime(Vcb->McbTree->Inode.i_mtime);
2677 Vcb->McbTree->ChangeTime = Ext2NtTime(Vcb->McbTree->Inode.i_mtime);
2678
2679 /* check bitmap if user specifies it */
2680 if (IsFlagOn(Ext2Global->Flags, EXT2_CHECKING_BITMAP)) {
2681 Ext2CheckBitmapConsistency(IrpContext, Vcb);
2682 }
2683
2684 /* get anything doen, then refer target device */
2685 ObReferenceObject(Vcb->TargetDeviceObject);
2686
2687 /* query parameters from registry */
2688 Ext2PerformRegistryVolumeParams(Vcb);
2689
2690 SetLongFlag(Vcb->Flags, VCB_INITIALIZED);
2691
2692 } _SEH2_FINALLY {
2693
2694 if (!NT_SUCCESS(Status)) {
2695
2696 if (Vcb->McbTree) {
2697 Ext2FreeMcb(Vcb, Vcb->McbTree);
2698 }
2699
2700 if (InodeLookasideInitialized) {
2701 ExDeleteNPagedLookasideList(&(Vcb->InodeLookasideList));
2702 }
2703
2704 if (ExtentsInitialized) {
2705 if (Vcb->bd.bd_bh_cache) {
2706 if (GroupLoaded)
2707 Ext2PutGroup(Vcb);
2708 kmem_cache_destroy(Vcb->bd.bd_bh_cache);
2709 }
2710 FsRtlUninitializeLargeMcb(&(Vcb->Extents));
2711 }
2712
2713 if (Vcb->Volume) {
2714 if (Vcb->Volume->PrivateCacheMap) {
2715 Ext2SyncUninitializeCacheMap(Vcb->Volume);
2716 }
2717 ObDereferenceObject(Vcb->Volume);
2718 }
2719
2720 if (NotifySyncInitialized) {
2721 FsRtlNotifyUninitializeSync(&Vcb->NotifySync);
2722 }
2723
2724 if (VcbResourceInitialized) {
2725 ExDeleteResourceLite(&Vcb->McbLock);
2726 ExDeleteResourceLite(&Vcb->MetaInode);
2727 ExDeleteResourceLite(&Vcb->MetaBlock);
2728 ExDeleteResourceLite(&Vcb->sbi.s_gd_lock);
2729 ExDeleteResourceLite(&Vcb->MainResource);
2730 ExDeleteResourceLite(&Vcb->PagingIoResource);
2731 }
2732 }
2733 } _SEH2_END;
2734
2735 return Status;
2736 }
2737
2738
2739 VOID
2740 Ext2TearDownStream(IN PEXT2_VCB Vcb)
2741 {
2742 PFILE_OBJECT Stream = Vcb->Volume;
2743 IO_STATUS_BLOCK IoStatus;
2744
2745 ASSERT(Vcb != NULL);
2746 ASSERT((Vcb->Identifier.Type == EXT2VCB) &&
2747 (Vcb->Identifier.Size == sizeof(EXT2_VCB)));
2748
2749 if (Stream) {
2750
2751 Vcb->Volume = NULL;
2752
2753 if (IsFlagOn(Stream->Flags, FO_FILE_MODIFIED)) {
2754 CcFlushCache(&(Vcb->SectionObject), NULL, 0, &IoStatus);
2755 ClearFlag(Stream->Flags, FO_FILE_MODIFIED);
2756 }
2757
2758 if (Stream->PrivateCacheMap) {
2759 Ext2SyncUninitializeCacheMap(Stream);
2760 }
2761
2762 ObDereferenceObject(Stream);
2763 }
2764 }
2765
2766 VOID
2767 Ext2DestroyVcb (IN PEXT2_VCB Vcb)
2768 {
2769 ASSERT(Vcb != NULL);
2770 ASSERT((Vcb->Identifier.Type == EXT2VCB) &&
2771 (Vcb->Identifier.Size == sizeof(EXT2_VCB)));
2772
2773 DEBUG(DL_FUN, ("Ext2DestroyVcb ...\n"));
2774
2775 if (Vcb->Volume) {
2776 Ext2TearDownStream(Vcb);
2777 }
2778 ASSERT(NULL == Vcb->Volume);
2779
2780 FsRtlNotifyUninitializeSync(&Vcb->NotifySync);
2781 Ext2ListExtents(&Vcb->Extents);
2782 FsRtlUninitializeLargeMcb(&(Vcb->Extents));
2783
2784 Ext2CleanupAllMcbs(Vcb);
2785
2786 Ext2DropGroup(Vcb);
2787
2788 if (Vcb->bd.bd_bh_cache)
2789 kmem_cache_destroy(Vcb->bd.bd_bh_cache);
2790 ExDeleteResourceLite(&Vcb->bd.bd_bh_lock);
2791
2792 if (Vcb->SuperBlock) {
2793 Ext2FreePool(Vcb->SuperBlock, EXT2_SB_MAGIC);
2794 Vcb->SuperBlock = NULL;
2795 }
2796
2797 if (IsFlagOn(Vcb->Flags, VCB_NEW_VPB)) {
2798 ASSERT(Vcb->Vpb2 != NULL);
2799 DEBUG(DL_DBG, ("Ext2DestroyVcb: Vpb2 to be freed: %p\n", Vcb->Vpb2));
2800 ExFreePoolWithTag(Vcb->Vpb2, TAG_VPB);
2801 DEC_MEM_COUNT(PS_VPB, Vcb->Vpb2, sizeof(VPB));
2802 Vcb->Vpb2 = NULL;
2803 }
2804
2805 ObDereferenceObject(Vcb->TargetDeviceObject);
2806
2807 ExDeleteNPagedLookasideList(&(Vcb->InodeLookasideList));
2808 ExDeleteResourceLite(&Vcb->McbLock);
2809 ExDeleteResourceLite(&Vcb->MetaInode);
2810 ExDeleteResourceLite(&Vcb->MetaBlock);
2811 ExDeleteResourceLite(&Vcb->sbi.s_gd_lock);
2812 ExDeleteResourceLite(&Vcb->PagingIoResource);
2813 ExDeleteResourceLite(&Vcb->MainResource);
2814
2815 DEBUG(DL_DBG, ("Ext2DestroyVcb: DevObject=%p Vcb=%p\n", Vcb->DeviceObject, Vcb));
2816 IoDeleteDevice(Vcb->DeviceObject);
2817 DEC_MEM_COUNT(PS_VCB, Vcb->DeviceObject, sizeof(EXT2_VCB));
2818 }
2819
2820
2821 /* uninitialize cache map */
2822
2823 VOID
2824 Ext2SyncUninitializeCacheMap (
2825 IN PFILE_OBJECT FileObject
2826 )
2827 {
2828 CACHE_UNINITIALIZE_EVENT UninitializeCompleteEvent;
2829 NTSTATUS WaitStatus;
2830 LARGE_INTEGER Ext2LargeZero = {0,0};
2831
2832
2833 KeInitializeEvent( &UninitializeCompleteEvent.Event,
2834 SynchronizationEvent,
2835 FALSE);
2836
2837 CcUninitializeCacheMap( FileObject,
2838 &Ext2LargeZero,
2839 &UninitializeCompleteEvent );
2840
2841 WaitStatus = KeWaitForSingleObject( &UninitializeCompleteEvent.Event,
2842 Executive,
2843 KernelMode,
2844 FALSE,
2845 NULL);
2846
2847 ASSERT (NT_SUCCESS(WaitStatus));
2848 }
2849
2850 /* Link Mcb to tail of Vcb->McbList queue */
2851
2852 VOID
2853 Ext2LinkTailMcb(PEXT2_VCB Vcb, PEXT2_MCB Mcb)
2854 {
2855 if (Mcb->Inode.i_ino == EXT2_ROOT_INO) {
2856 return;
2857 }
2858
2859 ExAcquireResourceExclusiveLite(&Vcb->McbLock, TRUE);
2860
2861 if (IsFlagOn(Mcb->Flags, MCB_VCB_LINK)) {
2862 DEBUG(DL_RES, ( "Ext2LinkTailMcb: %wZ already linked.\n",
2863 &Mcb->FullName));
2864 } else {
2865 InsertTailList(&Vcb->McbList, &Mcb->Link);
2866 SetLongFlag(Mcb->Flags, MCB_VCB_LINK);
2867 Ext2ReferXcb(&Vcb->NumOfMcb);
2868 }
2869
2870 ExReleaseResourceLite(&Vcb->McbLock);
2871 }
2872
2873 /* Link Mcb to head of Vcb->McbList queue */
2874
2875 VOID
2876 Ext2LinkHeadMcb(PEXT2_VCB Vcb, PEXT2_MCB Mcb)
2877 {
2878 if (Mcb->Inode.i_ino == EXT2_ROOT_INO) {
2879 return;
2880 }
2881
2882 ExAcquireResourceExclusiveLite(&Vcb->McbLock, TRUE);
2883
2884 if (!IsFlagOn(Mcb->Flags, MCB_VCB_LINK)) {
2885 InsertHeadList(&Vcb->McbList, &Mcb->Link);
2886 SetLongFlag(Mcb->Flags, MCB_VCB_LINK);
2887 Ext2ReferXcb(&Vcb->NumOfMcb);
2888 } else {
2889 DEBUG(DL_RES, ( "Ext2LinkHeadMcb: %wZ already linked.\n",
2890 &Mcb->FullName));
2891 }
2892 ExReleaseResourceLite(&Vcb->McbLock);
2893 }
2894
2895 /* Unlink Mcb from Vcb->McbList queue */
2896
2897 VOID
2898 Ext2UnlinkMcb(PEXT2_VCB Vcb, PEXT2_MCB Mcb)
2899 {
2900 if (Mcb->Inode.i_ino == EXT2_ROOT_INO) {
2901 return;
2902 }
2903
2904 ExAcquireResourceExclusiveLite(&Vcb->McbLock, TRUE);
2905
2906 if (IsFlagOn(Mcb->Flags, MCB_VCB_LINK)) {
2907 RemoveEntryList(&(Mcb->Link));
2908 ClearLongFlag(Mcb->Flags, MCB_VCB_LINK);
2909 Ext2DerefXcb(&Vcb->NumOfMcb);
2910 } else {
2911 DEBUG(DL_RES, ( "Ext2UnlinkMcb: %wZ already unlinked.\n",
2912 &Mcb->FullName));
2913 }
2914 ExReleaseResourceLite(&Vcb->McbLock);
2915 }
2916
2917 /* get the first Mcb record in Vcb->McbList */
2918
2919 PEXT2_MCB
2920 Ext2FirstUnusedMcb(PEXT2_VCB Vcb, BOOLEAN Wait, ULONG Number)
2921 {
2922 PEXT2_MCB Head = NULL;
2923 PEXT2_MCB Tail = NULL;
2924 PEXT2_MCB Mcb = NULL;
2925 PLIST_ENTRY List = NULL;
2926 ULONG i = 0;
2927
2928 if (!ExAcquireResourceExclusiveLite(&Vcb->McbLock, Wait)) {
2929 return NULL;
2930 }
2931
2932 while (Number--) {
2933
2934 if (!IsListEmpty(&Vcb->McbList)) {
2935
2936 while (i++ < Vcb->NumOfMcb) {
2937
2938 List = RemoveHeadList(&Vcb->McbList);
2939 Mcb = CONTAINING_RECORD(List, EXT2_MCB, Link);
2940 ASSERT(IsFlagOn(Mcb->Flags, MCB_VCB_LINK));
2941
2942 if (Mcb->Fcb == NULL && !IsMcbRoot(Mcb) &&
2943 Mcb->Refercount == 0 &&
2944 (Mcb->Child == NULL || IsMcbSymLink(Mcb))) {
2945
2946 Ext2RemoveMcb(Vcb, Mcb);
2947 ClearLongFlag(Mcb->Flags, MCB_VCB_LINK);
2948 Ext2DerefXcb(&Vcb->NumOfMcb);
2949
2950 /* attach all Mcb into a chain*/
2951 if (Head) {
2952 ASSERT(Tail != NULL);
2953 Tail->Next = Mcb;
2954 Tail = Mcb;
2955 } else {
2956 Head = Tail = Mcb;
2957 }
2958 Tail->Next = NULL;
2959
2960 } else {
2961
2962 InsertTailList(&Vcb->McbList, &Mcb->Link);
2963 Mcb = NULL;
2964 }
2965 }
2966 }
2967 }
2968 ExReleaseResourceLite(&Vcb->McbLock);
2969
2970 return Head;
2971 }
2972
2973
2974 /* Reaper thread to release unused Mcb blocks */
2975 VOID NTAPI
2976 Ext2McbReaperThread(
2977 PVOID Context
2978 )
2979 {
2980 PEXT2_REAPER Reaper = Context;
2981 PLIST_ENTRY List = NULL;
2982 LARGE_INTEGER Timeout;
2983
2984 PEXT2_VCB Vcb = NULL;
2985 PEXT2_MCB Mcb = NULL;
2986
2987 ULONG i, NumOfMcbs;
2988
2989 BOOLEAN GlobalAcquired = FALSE;
2990
2991 BOOLEAN DidNothing = TRUE;
2992 BOOLEAN LastState = TRUE;
2993 BOOLEAN WaitLock;
2994
2995 _SEH2_TRY {
2996
2997 /* wake up DirverEntry */
2998 KeSetEvent(&Reaper->Engine, 0, FALSE);
2999
3000 /* now process looping */
3001 while (!IsFlagOn(Reaper->Flags, EXT2_REAPER_FLAG_STOP)) {
3002
3003 WaitLock = FALSE;
3004
3005 /* calculate how long we need wait */
3006 if (Ext2Global->PerfStat.Current.Mcb > (((ULONG)Ext2Global->MaxDepth) * 128)) {
3007 Timeout.QuadPart = (LONGLONG)-1000*1000; /* 0.1 second */
3008 NumOfMcbs = Ext2Global->MaxDepth * 4;
3009 WaitLock = TRUE;
3010 } else if (Ext2Global->PerfStat.Current.Mcb > (((ULONG)Ext2Global->MaxDepth) * 32)) {
3011 Timeout.QuadPart = (LONGLONG)-1000*1000*5; /* 0.5 second */
3012 NumOfMcbs = Ext2Global->MaxDepth * 2;
3013 WaitLock = TRUE;
3014 } else if (Ext2Global->PerfStat.Current.Mcb > (((ULONG)Ext2Global->MaxDepth) * 8)) {
3015 Timeout.QuadPart = (LONGLONG)-1000*1000*10; /* 1 second */
3016 NumOfMcbs = Ext2Global->MaxDepth;
3017 } else if (Ext2Global->PerfStat.Current.Mcb > (((ULONG)Ext2Global->MaxDepth) * 2)) {
3018 Timeout.QuadPart = (LONGLONG)-2*1000*1000*10; /* 2 second */
3019 NumOfMcbs = Ext2Global->MaxDepth / 4;
3020 } else if (Ext2Global->PerfStat.Current.Mcb > (ULONG)Ext2Global->MaxDepth) {
3021 Timeout.QuadPart = (LONGLONG)-4*1000*1000*10; /* 4 seconds */
3022 NumOfMcbs = Ext2Global->MaxDepth / 8;
3023 } else if (DidNothing) {
3024 Timeout.QuadPart = (LONGLONG)-8*1000*1000*10; /* 8 seconds */
3025 if (LastState) {
3026 Timeout.QuadPart *= 2;
3027 }
3028 NumOfMcbs = Ext2Global->MaxDepth / 16;
3029 } else {
3030 Timeout.QuadPart = (LONGLONG)-5*1000*1000*10; /* 5 seconds */
3031 if (LastState) {
3032 Timeout.QuadPart *= 2;
3033 }
3034 NumOfMcbs = Ext2Global->MaxDepth / 32;
3035 }
3036
3037 if (NumOfMcbs == 0)
3038 NumOfMcbs = 1;
3039
3040 LastState = DidNothing;
3041
3042 /* wait until it is waken or it times out */
3043 KeWaitForSingleObject(
3044 &Reaper->Wait,
3045 Executive,
3046 KernelMode,
3047 FALSE,
3048 &Timeout
3049 );
3050
3051 if (IsFlagOn(Reaper->Flags, EXT2_REAPER_FLAG_STOP))
3052 break;
3053
3054 DidNothing = TRUE;
3055
3056 /* acquire global exclusive lock */
3057 if (!ExAcquireResourceSharedLite(&Ext2Global->Resource, WaitLock)) {
3058 continue;
3059 }
3060 GlobalAcquired = TRUE;
3061
3062 /* search all Vcb to get unused resources freed to system */
3063 for (List = Ext2Global->VcbList.Flink;
3064 List != &(Ext2Global->VcbList);
3065 List = List->Flink ) {
3066
3067 Vcb = CONTAINING_RECORD(List, EXT2_VCB, Next);
3068
3069 Mcb = Ext2FirstUnusedMcb(Vcb, WaitLock, NumOfMcbs);
3070 while (Mcb) {
3071 PEXT2_MCB Next = Mcb->Next;
3072 DEBUG(DL_RES, ( "Ext2ReaperThread: releasing Mcb (%p): %wZ"
3073 " Total: %xh\n", Mcb, &Mcb->FullName,
3074 Ext2Global->PerfStat.Current.Mcb));
3075 Ext2FreeMcb(Vcb, Mcb);
3076 Mcb = Next;
3077 LastState = DidNothing = FALSE;
3078 }
3079 }
3080
3081 if (GlobalAcquired) {
3082 ExReleaseResourceLite(&Ext2Global->Resource);
3083 GlobalAcquired = FALSE;
3084 }
3085 }
3086
3087 } _SEH2_FINALLY {
3088
3089 if (GlobalAcquired) {
3090 ExReleaseResourceLite(&Ext2Global->Resource);
3091 }
3092
3093 KeSetEvent(&Reaper->Engine, 0, FALSE);
3094 } _SEH2_END;
3095
3096 PsTerminateSystemThread(STATUS_SUCCESS);
3097 }
3098
3099
3100 /* get the first Mcb record in Vcb->McbList */
3101
3102 BOOLEAN
3103 Ext2QueryUnusedBH(PEXT2_VCB Vcb, PLIST_ENTRY head)
3104 {
3105 struct buffer_head *bh = NULL;
3106 PLIST_ENTRY list = NULL;
3107 LARGE_INTEGER now;
3108 BOOLEAN wake = FALSE;
3109
3110 KeQuerySystemTime(&now);
3111
3112 ExAcquireResourceExclusiveLite(&Vcb->bd.bd_bh_lock, TRUE);
3113 while (!IsListEmpty(&Vcb->bd.bd_bh_free)) {
3114 list = RemoveHeadList(&Vcb->bd.bd_bh_free);
3115 bh = CONTAINING_RECORD(list, struct buffer_head, b_link);
3116 if (atomic_read(&bh->b_count)) {
3117 InitializeListHead(&bh->b_link);
3118 continue;
3119 }
3120
3121 if ( IsFlagOn(Vcb->Flags, VCB_BEING_DROPPED) ||
3122 (bh->b_ts_drop.QuadPart + (LONGLONG)10*1000*1000*15) > now.QuadPart ||
3123 (bh->b_ts_creat.QuadPart + (LONGLONG)10*1000*1000*180) > now.QuadPart) {
3124 InsertTailList(head, &bh->b_link);
3125 } else {
3126 InsertHeadList(&Vcb->bd.bd_bh_free, &bh->b_link);
3127 break;
3128 }
3129 }
3130 wake