2 * reactos Cache Manager (Cc*) SharedCacheMap structure of libcaptive
3 * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
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; exactly version 2 of June 1991 is required
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "sharedcachemap.h" /* self */
23 #include "sharedcachemap-priv.h" /* self */
25 #include "marshallers.h"
26 #include <glib-object.h>
27 #include "privatebcbpin.h"
28 #include "captive/macros.h"
30 #include "reactos/ddk/obfuncs.h"
33 static GHashTable *CaptiveSharedCacheMapObject_hash;
35 static void CaptiveSharedCacheMapObject_hash_init(void)
37 if (CaptiveSharedCacheMapObject_hash)
39 CaptiveSharedCacheMapObject_hash=g_hash_table_new(
40 g_direct_hash, /* hash_func */
41 g_direct_equal); /* key_equal_func */
45 static gpointer captive_shared_cache_map_object_parent_class=NULL;
48 static void captive_shared_cache_map_object_finalize(CaptiveSharedCacheMapObject *captive_shared_cache_map_object)
50 static const CC_FILE_SIZES FileSizes_zero;
53 CaptiveSharedCacheMapObject_page *page;
55 g_return_if_fail(captive_shared_cache_map_object!=NULL);
57 for (offset=0;offset<CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE);offset+=PAGE_SIZE) {
58 page=captive_shared_cache_map_object->pages+offset/PAGE_SIZE;
59 if (!page->data_valid)
61 g_assert(!page->dirty); /* FIXME: Is it allowed by W32? */
64 CaptiveSharedCacheMapObject_hash_init();
65 errbool=g_hash_table_remove(CaptiveSharedCacheMapObject_hash,captive_shared_cache_map_object);
66 g_assert(errbool==TRUE);
68 captive_shared_cache_map_FileSizes_set(captive_shared_cache_map_object,&FileSizes_zero);
69 g_assert(captive_shared_cache_map_object->buffer==NULL);
70 g_assert(captive_shared_cache_map_object->pages==NULL);
72 if (captive_shared_cache_map_object->pin_hash) {
73 captive_private_bcb_pin_object_hash_destroy(captive_shared_cache_map_object->pin_hash);
74 captive_shared_cache_map_object->pin_hash=NULL;
76 g_assert(captive_shared_cache_map_object->map==NULL);
77 if (captive_shared_cache_map_object->SectionObjectPointer) {
78 g_assert(captive_shared_cache_map_object==captive_shared_cache_map_object->SectionObjectPointer->SharedCacheMap);
79 captive_shared_cache_map_object->SectionObjectPointer->SharedCacheMap=NULL;
82 if (captive_shared_cache_map_object->FileObject) {
83 /* W32 dereferences twice. */
84 ObDereferenceObject(captive_shared_cache_map_object->FileObject);
85 captive_shared_cache_map_object->FileObject=NULL;
88 G_OBJECT_CLASS(captive_shared_cache_map_object_parent_class)->finalize((GObject *)captive_shared_cache_map_object);
91 static guint FileSizes_changed_signal;
92 static guint purge_signal;
94 static void captive_shared_cache_map_object_FileSizes_changed(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
95 guint64 AllocationSize,guint64 FileSize,guint64 ValidDataLength);
96 static void captive_shared_cache_map_object_purge(CaptiveSharedCacheMapObject *captive_shared_cache_map_object);
98 static void captive_shared_cache_map_object_class_init(CaptiveSharedCacheMapObjectClass *class)
100 GObjectClass *gobject_class=G_OBJECT_CLASS(class);
102 captive_shared_cache_map_object_parent_class=g_type_class_ref(g_type_parent(G_TYPE_FROM_CLASS(class)));
103 gobject_class->finalize=(void (*)(GObject *object))captive_shared_cache_map_object_finalize;
105 class->FileSizes_changed=captive_shared_cache_map_object_FileSizes_changed;
106 class->purge=captive_shared_cache_map_object_purge;
108 FileSizes_changed_signal=g_signal_new("FileSizes_changed",
109 G_TYPE_FROM_CLASS(gobject_class),
111 G_STRUCT_OFFSET(CaptiveSharedCacheMapObjectClass,FileSizes_changed),
113 captive_cc_VOID__UINT64_UINT64_UINT64,
114 G_TYPE_NONE,3,G_TYPE_UINT64,G_TYPE_UINT64,G_TYPE_UINT64);
115 purge_signal=g_signal_new("purge",
116 G_TYPE_FROM_CLASS(gobject_class),
118 G_STRUCT_OFFSET(CaptiveSharedCacheMapObjectClass,purge),
120 captive_cc_VOID__VOID,
125 static void captive_shared_cache_map_object_init(CaptiveSharedCacheMapObject *captive_shared_cache_map_object)
127 captive_shared_cache_map_object->pin_hash=captive_private_bcb_pin_object_hash_new();
129 captive_shared_cache_map_w32_ref(captive_shared_cache_map_object);
130 g_object_unref(captive_shared_cache_map_object); /* ==sink */
132 CaptiveSharedCacheMapObject_hash_init();
133 g_hash_table_insert(CaptiveSharedCacheMapObject_hash,
134 captive_shared_cache_map_object,captive_shared_cache_map_object);
138 GType captive_shared_cache_map_object_get_type(void)
140 static GType captive_shared_cache_map_object_type=0;
142 if (!captive_shared_cache_map_object_type) {
143 static const GTypeInfo captive_shared_cache_map_object_info={
144 sizeof(CaptiveSharedCacheMapObjectClass),
145 NULL, /* base_init */
146 NULL, /* base_finalize */
147 (GClassInitFunc)captive_shared_cache_map_object_class_init,
148 NULL, /* class_finalize */
149 NULL, /* class_data */
150 sizeof(CaptiveSharedCacheMapObject),
152 (GInstanceInitFunc)captive_shared_cache_map_object_init,
155 captive_shared_cache_map_object_type=g_type_register_static(G_TYPE_OBJECT,
156 "CaptiveSharedCacheMapObject",&captive_shared_cache_map_object_info,0);
159 return captive_shared_cache_map_object_type;
162 static void captive_shared_cache_map_object_FileSizes_changed(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
163 guint64 AllocationSize,guint64 FileSize,guint64 ValidDataLength)
165 size_t size_old,size_new;
166 guint64 size64_old,size64_new;
168 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
170 g_assert((!captive_shared_cache_map_object->buffer)==(!captive_shared_cache_map_object->AllocationSize));
171 g_assert((!captive_shared_cache_map_object->pages)==(!captive_shared_cache_map_object->AllocationSize));
173 size64_old=CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE);
175 g_assert(size_old==size64_old);
176 size64_new=CAPTIVE_ROUND_UP64(AllocationSize,PAGE_SIZE);
178 if (size_new!=size64_new) {
180 g_error("Mapped size %" G_GUINT64_FORMAT " is too big; FIXME: non-mmap(2)-able areas not yet supported",size64_new);
181 g_assert_not_reached();
184 if (size_old!=size_new) {
187 /* These two assertions should be already catched by pin/map signal handlers. */
188 g_assert(!captive_shared_cache_map_object->map);
189 g_assert(!g_hash_table_size(captive_shared_cache_map_object->pin_hash));
191 if (AllocationSize) {
197 PAGE_SIZE+size_new+PAGE_SIZE, /* length; leading and trailing boundary check pages */
198 PROT_READ|PROT_WRITE, /* prot; read/write must be possible although write is not guaranteed to be flushed yet */
199 MAP_PRIVATE|MAP_ANONYMOUS /* flags */
200 |MAP_NORESERVE, /* At least ext2fsd maps the whole disk. */
201 -1, /* fd; ignored due to MAP_ANONYMOUS */
202 0); /* offset; ignored due to MAP_ANONYMOUS */
203 if (base==MAP_FAILED)
205 g_assert(base!=NULL);
208 errint=munmap(base-PAGE_SIZE,PAGE_SIZE); /* unmap leading boundary check page */
210 errint=munmap(base+size_new,PAGE_SIZE); /* unmap trailing boundary check page */
218 memcpy(buffer_new,captive_shared_cache_map_object->buffer,
219 MIN(AllocationSize,captive_shared_cache_map_object->AllocationSize));
221 if (captive_shared_cache_map_object->AllocationSize) {
224 errint=munmap(captive_shared_cache_map_object->buffer,size_old);
228 captive_shared_cache_map_object->buffer=buffer_new;
230 #if 0 /* It appears it is valid to squeeze out 'dirty' blocks. FIXME: Flush them? */
231 if (size_old>size_new) {
234 for (now=size_new;now<size_old;now+=PAGE_SIZE) {
235 if (!captive_shared_cache_map_object->pages[now/PAGE_SIZE].data_valid)
237 g_assert(!captive_shared_cache_map_object->pages[now/PAGE_SIZE].dirty);
241 captive_shared_cache_map_object->pages=g_realloc(captive_shared_cache_map_object->pages,
242 size_new/PAGE_SIZE*sizeof(*captive_shared_cache_map_object->pages));
243 if (size_new>size_old) /* prevent 'size_new-size_old' as it is unsigned! */
244 memset(captive_shared_cache_map_object->pages+(size_old/PAGE_SIZE),0,
245 (size_new-size_old)/PAGE_SIZE*sizeof(*captive_shared_cache_map_object->pages));
248 captive_shared_cache_map_object->AllocationSize=AllocationSize;
249 captive_shared_cache_map_object->FileSize=FileSize;
250 captive_shared_cache_map_object->ValidDataLength=ValidDataLength;
252 g_assert((!captive_shared_cache_map_object->buffer)==(!captive_shared_cache_map_object->AllocationSize));
253 g_assert((!captive_shared_cache_map_object->pages)==(!captive_shared_cache_map_object->AllocationSize));
256 static void captive_shared_cache_map_object_purge(CaptiveSharedCacheMapObject *captive_shared_cache_map_object)
258 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
260 /* NOP; just to provide slot for checking by Bcbs */
263 CaptiveSharedCacheMapObject *captive_shared_cache_map_get_ref(FILE_OBJECT *FileObject,
264 const CC_FILE_SIZES *FileSizes,BOOLEAN PinAccess,const CACHE_MANAGER_CALLBACKS *CallBacks,VOID *LazyWriterContext)
266 CaptiveSharedCacheMapObject *captive_shared_cache_map_object;
268 g_return_val_if_fail(FileObject!=NULL,NULL);
269 g_return_val_if_fail(FileObject->SectionObjectPointer!=NULL,NULL);
270 g_return_val_if_fail(FileSizes!=NULL,NULL);
271 g_return_val_if_fail(CallBacks!=NULL,NULL);
273 if ((captive_shared_cache_map_object=FileObject->SectionObjectPointer->SharedCacheMap)) {
274 captive_shared_cache_map_w32_ref(captive_shared_cache_map_object);
277 captive_shared_cache_map_object=g_object_new(
278 CAPTIVE_SHARED_CACHE_MAP_TYPE_OBJECT, /* object_type */
279 NULL); /* first_property_name; FIXME: support properties */
281 /* FIXME: When to drop SharedCacheMap?
282 * Currently we never close it.
283 * Fix also CcZeroData() workaround.
285 g_object_ref(captive_shared_cache_map_object);
287 /* W32 references twice. */
288 ObReferenceObject(FileObject);
289 captive_shared_cache_map_object->FileObject=FileObject;
290 captive_shared_cache_map_object->SectionObjectPointer=FileObject->SectionObjectPointer;
291 captive_shared_cache_map_object->AllocationSize=0;
292 captive_shared_cache_map_object->FileSize=0;
293 captive_shared_cache_map_object->ValidDataLength=0;
294 captive_shared_cache_map_object->PinAccess=PinAccess;
295 captive_shared_cache_map_object->CallBacks=*CallBacks;
296 captive_shared_cache_map_object->LazyWriterContext=LazyWriterContext;
298 FileObject->SectionObjectPointer->SharedCacheMap=captive_shared_cache_map_object;
301 g_assert(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
302 /* FileObject may differ - we can have a different reference to the same FCB. */
303 g_assert(FileObject->SectionObjectPointer==captive_shared_cache_map_object->SectionObjectPointer);
304 g_assert(PinAccess==captive_shared_cache_map_object->PinAccess);
305 g_assert(CallBacks->AcquireForLazyWrite==captive_shared_cache_map_object->CallBacks.AcquireForLazyWrite);
306 g_assert(CallBacks->ReleaseFromLazyWrite==captive_shared_cache_map_object->CallBacks.ReleaseFromLazyWrite);
307 g_assert(CallBacks->AcquireForReadAhead==captive_shared_cache_map_object->CallBacks.AcquireForReadAhead);
308 g_assert(CallBacks->ReleaseFromReadAhead==captive_shared_cache_map_object->CallBacks.ReleaseFromReadAhead);
309 g_assert(LazyWriterContext==captive_shared_cache_map_object->LazyWriterContext);
311 captive_shared_cache_map_FileSizes_set(captive_shared_cache_map_object,FileSizes);
313 return captive_shared_cache_map_object;
316 void captive_shared_cache_map_FileSizes_set(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
317 const CC_FILE_SIZES *FileSizes)
319 guint64 AllocationSize,FileSize,ValidDataLength;
321 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
322 g_return_if_fail(FileSizes!=NULL);
324 AllocationSize=FileSizes->AllocationSize.QuadPart;
325 FileSize=FileSizes->FileSize.QuadPart;
326 ValidDataLength=FileSizes->ValidDataLength.QuadPart;
328 if (ValidDataLength==G_MAXINT64)
329 ValidDataLength=FileSize;
331 g_assert(AllocationSize>=0);
332 g_assert(FileSize>=0);
333 g_assert(ValidDataLength>=0);
335 g_assert(ValidDataLength<=FileSize);
336 /* Do not: g_assert(0==(AllocationSize%0x200));
337 * as it is true for ntfs.sys of NT-5.1sp1 but it fails for fastfat.sys of NT-5.1sp1.
339 /* AllocationSize can be much higher: */
340 g_assert(FileSize<=AllocationSize);
342 /* Prevent signalling if not needed. */
344 || captive_shared_cache_map_object->AllocationSize!=AllocationSize
345 || captive_shared_cache_map_object->FileSize!=FileSize
346 || captive_shared_cache_map_object->ValidDataLength!=ValidDataLength) {
347 /* Signalling is forbidden in captive_shared_cache_map_object_finalize(). */
348 if (G_OBJECT(captive_shared_cache_map_object)->ref_count==0) {
349 (*CAPTIVE_SHARED_CACHE_MAP_OBJECT_GET_CLASS(captive_shared_cache_map_object)->FileSizes_changed)
350 (captive_shared_cache_map_object,AllocationSize,FileSize,ValidDataLength);
353 g_signal_emit(captive_shared_cache_map_object,FileSizes_changed_signal,0,
354 AllocationSize,FileSize,ValidDataLength);
358 g_assert(captive_shared_cache_map_object->AllocationSize==AllocationSize);
359 g_assert(captive_shared_cache_map_object->FileSize==FileSize);
360 g_assert(captive_shared_cache_map_object->ValidDataLength==ValidDataLength);
363 CaptiveSharedCacheMapObject *captive_SectionObjectPointers_to_SharedCacheMap(SECTION_OBJECT_POINTERS *SectionObjectPointers)
365 g_return_val_if_fail(SectionObjectPointers!=NULL,NULL);
366 g_return_val_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(SectionObjectPointers->SharedCacheMap),NULL);
368 return SectionObjectPointers->SharedCacheMap;
371 CaptiveSharedCacheMapObject *captive_FileObject_to_SharedCacheMap(FILE_OBJECT *FileObject)
373 g_return_val_if_fail(FileObject!=NULL,NULL);
375 return captive_SectionObjectPointers_to_SharedCacheMap(FileObject->SectionObjectPointer);
378 void captive_shared_cache_map_w32_ref(CaptiveSharedCacheMapObject *captive_shared_cache_map_object)
380 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
382 g_object_ref(captive_shared_cache_map_object);
383 captive_shared_cache_map_object->w32_ref_count++;
386 void captive_shared_cache_map_w32_unref(CaptiveSharedCacheMapObject *captive_shared_cache_map_object)
388 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
389 g_return_if_fail(G_OBJECT(captive_shared_cache_map_object)->ref_count>0);
391 captive_shared_cache_map_object->w32_ref_count--;
392 g_object_unref(captive_shared_cache_map_object);
395 void captive_shared_cache_map_data_validate_read(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
396 FILE_OBJECT *FileObject,guint64 start,guint64 end)
399 gboolean after_eof=FALSE; /* Did we reached the end of file already? */
401 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
402 g_return_if_fail(captive_shared_cache_map_object==captive_FileObject_to_SharedCacheMap(FileObject));
403 g_return_if_fail(start<=end);
404 g_return_if_fail(end<=CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE));
406 start=CAPTIVE_ROUND_DOWN64(start,PAGE_SIZE);
407 end=CAPTIVE_ROUND_UP64(end,PAGE_SIZE);
409 for (now=start;now<end;now+=PAGE_SIZE) {
410 LARGE_INTEGER now_LargeInteger;
413 if (captive_shared_cache_map_object->pages[now/PAGE_SIZE].data_valid)
415 now_LargeInteger.QuadPart=now;
416 got=captive_Cc_IoPageRead(FileObject,captive_shared_cache_map_object->buffer+now,PAGE_SIZE,&now_LargeInteger);
420 g_assert(got<=PAGE_SIZE);
421 after_eof=(got<PAGE_SIZE);
422 captive_shared_cache_map_object->pages[now/PAGE_SIZE].data_valid=TRUE;
423 captive_shared_cache_map_object->pages[now/PAGE_SIZE].dirty=FALSE;
427 void captive_shared_cache_map_data_validate_noread(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
428 guint64 start,guint64 end)
432 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
433 g_return_if_fail(start<=end);
434 g_return_if_fail(end<=CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE));
436 start=CAPTIVE_ROUND_DOWN64(start,PAGE_SIZE);
437 end=CAPTIVE_ROUND_UP64(end,PAGE_SIZE);
439 for (now=start;now<end;now+=PAGE_SIZE) {
440 g_assert(captive_shared_cache_map_object->pages[now/PAGE_SIZE].data_valid);
444 void captive_shared_cache_map_set_data_valid(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
445 guint64 start,guint64 end)
449 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
450 g_return_if_fail(start<=end);
451 g_return_if_fail(end<=CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE));
453 start=CAPTIVE_ROUND_UP64(start,PAGE_SIZE);
454 if (end<captive_shared_cache_map_object->FileSize)
455 end=CAPTIVE_ROUND_DOWN64(end,PAGE_SIZE);
457 /* We can validate the last page of the file
458 * even if it does not end on PAGE_SIZE boundary.
460 end=CAPTIVE_ROUND_UP64(end,PAGE_SIZE);
462 /* We may get end<start here - it is valid to not to validate anything. */
464 for (now=start;now<end;now+=PAGE_SIZE) {
465 captive_shared_cache_map_object->pages[now/PAGE_SIZE].data_valid=TRUE;
466 /* .dirty is undefined */
470 void captive_shared_cache_map_set_dirty(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
471 guint64 start,guint64 end)
474 CaptiveSharedCacheMapObject_page *page;
476 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
477 g_return_if_fail(end<=CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE));
479 start=CAPTIVE_ROUND_DOWN64(start,PAGE_SIZE);
480 end=CAPTIVE_ROUND_UP64(end,PAGE_SIZE);
482 for (now=start;now<end;now+=PAGE_SIZE) {
483 page=captive_shared_cache_map_object->pages+now/PAGE_SIZE;
484 g_assert(page->data_valid);
493 gboolean captive_shared_cache_map_is_page_dirty(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
496 CaptiveSharedCacheMapObject_page *page;
498 g_return_val_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object),FALSE);
499 g_return_val_if_fail(offset<CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE),FALSE);
500 g_return_val_if_fail(0==CAPTIVE_ROUND_DOWN_EXCEEDING64(offset,PAGE_SIZE),FALSE);
501 page=captive_shared_cache_map_object->pages+offset/PAGE_SIZE;
502 g_return_val_if_fail(page->data_valid,FALSE);
507 void captive_shared_cache_map_page_set_lsn(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
508 guint64 offset,gint64 lsn)
510 CaptiveSharedCacheMapObject_page *page;
512 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
513 g_return_if_fail(offset<=CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE));
514 g_return_if_fail(0==CAPTIVE_ROUND_DOWN_EXCEEDING64(offset,PAGE_SIZE));
515 page=captive_shared_cache_map_object->pages+offset/PAGE_SIZE;
516 g_return_if_fail(page->data_valid);
517 g_return_if_fail(page->dirty);
518 g_return_if_fail(page->lsn_oldest<=page->lsn_newest);
519 g_return_if_fail(!page->lsn_newest || lsn>=page->lsn_newest);
520 g_return_if_fail(captive_shared_cache_map_object->LogHandle_set);
521 g_return_if_fail(captive_shared_cache_map_object->FlushToLsnRoutine_set);
523 if (!page->lsn_oldest)
524 page->lsn_oldest=lsn;
525 page->lsn_newest=lsn;
528 void captive_shared_cache_map_purge(CaptiveSharedCacheMapObject *captive_shared_cache_map_object)
531 CaptiveSharedCacheMapObject_page *page;
533 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
535 g_signal_emit(captive_shared_cache_map_object,purge_signal,0);
537 /* Needed by fastfat.sys of NT-5.1sp1 during FAT32 unmount
538 * otherwise it fails on: g_assert(!page->dirty);
539 * It corrupts the disk if the buffer is dropped instead.
541 captive_shared_cache_map_flush(captive_shared_cache_map_object,
542 0,CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE));
544 for (offset=0;offset<CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE);offset+=PAGE_SIZE) {
545 page=captive_shared_cache_map_object->pages+offset/PAGE_SIZE;
546 if (!page->data_valid)
548 g_assert(!page->dirty);
549 page->data_valid=FALSE;
553 static VOID *captive_LogHandle;
554 static PFLUSH_TO_LSN captive_FlushToLsnRoutine;
556 void captive_shared_cache_map_set_LogHandle(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,VOID *LogHandle)
558 g_return_if_fail(!captive_shared_cache_map_object || CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
559 /* FIXME: 'captive_shared_cache_map_object->LogHandle_set' may be set.
560 * Does it mean 'LogHandle_set' is 'FileObject'-based instead of 'SharedCacheMap'-based?
565 g_assert(!captive_LogHandle || captive_LogHandle==LogHandle);
566 captive_LogHandle=LogHandle;
567 if (LogHandle && captive_shared_cache_map_object)
568 captive_shared_cache_map_object->LogHandle_set=TRUE;
571 void captive_shared_cache_map_set_FlushToLsnRoutine
572 (CaptiveSharedCacheMapObject *captive_shared_cache_map_object,PFLUSH_TO_LSN FlushToLsnRoutine)
574 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
575 /* FIXME: 'captive_shared_cache_map_object->FlushToLsnRoutine_set' may be set.
576 * Does it mean 'FlushToLsnRoutine_set' is 'FileObject'-based instead of 'SharedCacheMap'-based?
579 if (!FlushToLsnRoutine)
581 g_assert(!captive_FlushToLsnRoutine || captive_FlushToLsnRoutine==FlushToLsnRoutine);
582 captive_FlushToLsnRoutine=FlushToLsnRoutine;
583 if (FlushToLsnRoutine)
584 captive_shared_cache_map_object->FlushToLsnRoutine_set=TRUE;
587 static void captive_shared_cache_map_page_write(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
590 LARGE_INTEGER offset_LargeInteger;
591 static gint64 lsn_last;
592 CaptiveSharedCacheMapObject_page *page;
594 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
595 g_return_if_fail(captive_shared_cache_map_object->FileObject!=NULL);
596 g_return_if_fail(offset<=CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE));
597 g_return_if_fail(0==CAPTIVE_ROUND_DOWN_EXCEEDING64(offset,PAGE_SIZE));
598 page=captive_shared_cache_map_object->pages+offset/PAGE_SIZE;
599 g_return_if_fail(page->data_valid);
600 g_return_if_fail(page->dirty);
602 if (page->lsn_newest) {
604 g_assert(!lsn_last || lsn_last<=page->lsn_newest);
605 lsn_last=page->lsn_newest;
607 captive_stdcall_call_12((CaptiveStdCallFunc12)captive_FlushToLsnRoutine,
609 (gpointer)(guint32)(page->lsn_newest>> 0U), /* 'LARGE_INTEGER' argument */
610 (gpointer)(guint32)(page->lsn_newest>>32U));
613 offset_LargeInteger.QuadPart=offset;
614 captive_Cc_IoPageWrite(captive_shared_cache_map_object->FileObject,
615 captive_shared_cache_map_object->buffer+offset,PAGE_SIZE,&offset_LargeInteger);
622 typedef struct _captive_shared_cache_map_page_write_lsn_foreach_param captive_shared_cache_map_page_write_lsn_foreach_param;
623 struct _captive_shared_cache_map_page_write_lsn_foreach_param {
626 CaptiveSharedCacheMapObject *captive_shared_cache_map_object_best;
630 static void captive_shared_cache_map_page_write_lsn_foreach(
631 CaptiveSharedCacheMapObject *captive_shared_cache_map_object, /* key */
632 CaptiveSharedCacheMapObject *captive_shared_cache_map_object_value, /* value */
633 captive_shared_cache_map_page_write_lsn_foreach_param *param) /* user_data */
636 CaptiveSharedCacheMapObject_page *page;
638 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
639 g_return_if_fail(captive_shared_cache_map_object==captive_shared_cache_map_object_value);
640 g_return_if_fail(param!=NULL);
642 for (now=0;now<captive_shared_cache_map_object->AllocationSize;now+=PAGE_SIZE) {
643 page=captive_shared_cache_map_object->pages+now/PAGE_SIZE;
644 if (!page->data_valid)
648 if (!page->lsn_newest)
650 if (page->lsn_newest>=param->lsn_target)
652 if (param->lsn_best && page->lsn_newest>param->lsn_best)
654 param->lsn_best=page->lsn_newest;
655 param->captive_shared_cache_map_object_best=captive_shared_cache_map_object;
656 param->offset_best=now;
660 static void captive_shared_cache_map_page_write_lsn(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
663 CaptiveSharedCacheMapObject_page *page;
665 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
666 g_return_if_fail(captive_shared_cache_map_object->FileObject!=NULL);
667 g_return_if_fail(offset<CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE));
668 g_return_if_fail(0==CAPTIVE_ROUND_DOWN_EXCEEDING64(offset,PAGE_SIZE));
669 page=captive_shared_cache_map_object->pages+offset/PAGE_SIZE;
670 g_return_if_fail(page->data_valid);
671 g_return_if_fail(page->dirty);
673 if (page->lsn_newest) {
674 CaptiveSharedCacheMapObject_hash_init();
676 captive_shared_cache_map_page_write_lsn_foreach_param param;
678 param.lsn_target=page->lsn_newest;
680 g_hash_table_foreach(
681 CaptiveSharedCacheMapObject_hash, /* hash_table */
682 (GHFunc)captive_shared_cache_map_page_write_lsn_foreach, /* func */
683 ¶m); /* user_data */
686 captive_shared_cache_map_page_write(param.captive_shared_cache_map_object_best,param.offset_best);
690 captive_shared_cache_map_page_write(captive_shared_cache_map_object,offset);
693 guint64 captive_shared_cache_map_flush(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
694 guint64 start,guint64 end)
699 g_return_val_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object),0);
700 g_return_val_if_fail(start<=end,0);
702 end=MIN(end,CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE));
704 start=CAPTIVE_ROUND_DOWN64(start,PAGE_SIZE);
705 end=CAPTIVE_ROUND_UP64(end,PAGE_SIZE);
708 for (now=start;now<end;now+=PAGE_SIZE) {
709 if (!captive_shared_cache_map_object->pages[now/PAGE_SIZE].data_valid)
711 if (!captive_shared_cache_map_object->pages[now/PAGE_SIZE].dirty)
713 captive_shared_cache_map_page_write_lsn(captive_shared_cache_map_object,now);
717 /* We were calling W32 code - recheck our task completion. */
718 for (now=start;now<end;now+=PAGE_SIZE) {
719 if (!captive_shared_cache_map_object->pages[now/PAGE_SIZE].data_valid)
721 g_assert(!captive_shared_cache_map_object->pages[now/PAGE_SIZE].dirty);
727 static void captive_shared_cache_map_is_any_dirty_foreach(
728 CaptiveSharedCacheMapObject *captive_shared_cache_map_object, /* key */
729 CaptiveSharedCacheMapObject *captive_shared_cache_map_object_value, /* value */
730 gboolean *dirty_foundp) /* user_data */
734 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
735 g_return_if_fail(captive_shared_cache_map_object==captive_shared_cache_map_object_value);
736 g_return_if_fail(dirty_foundp!=NULL);
738 for (now=0;now<captive_shared_cache_map_object->AllocationSize;now+=PAGE_SIZE) {
739 if (!captive_shared_cache_map_object->pages[now/PAGE_SIZE].data_valid)
741 if (!captive_shared_cache_map_object->pages[now/PAGE_SIZE].dirty)
743 *dirty_foundp=TRUE; /* FIXME: stop the traversal. */
749 gboolean captive_shared_cache_map_is_any_dirty(void)
751 gboolean dirty_found;
753 CaptiveSharedCacheMapObject_hash_init();
755 g_hash_table_foreach(
756 CaptiveSharedCacheMapObject_hash, /* hash_table */
757 (GHFunc)captive_shared_cache_map_is_any_dirty_foreach, /* func */
758 &dirty_found); /* user_data */
764 typedef struct _captive_shared_cache_map_CcGetDirtyPages_foreach_param captive_shared_cache_map_CcGetDirtyPages_foreach_param;
765 struct _captive_shared_cache_map_CcGetDirtyPages_foreach_param {
766 PDIRTY_PAGE_ROUTINE DirtyPageRoutine;
772 static void captive_shared_cache_map_CcGetDirtyPages_foreach(
773 CaptiveSharedCacheMapObject *captive_shared_cache_map_object, /* key */
774 CaptiveSharedCacheMapObject *captive_shared_cache_map_object_value, /* value */
775 captive_shared_cache_map_CcGetDirtyPages_foreach_param *param) /* user_data */
778 CaptiveSharedCacheMapObject_page *page;
780 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
781 g_return_if_fail(captive_shared_cache_map_object->FileObject!=NULL);
782 g_return_if_fail(captive_shared_cache_map_object==captive_shared_cache_map_object_value);
783 g_return_if_fail(param!=NULL);
785 for (now=CAPTIVE_ROUND_DOWN64(captive_shared_cache_map_object->AllocationSize-1,PAGE_SIZE);now>=0;now-=PAGE_SIZE) {
786 LARGE_INTEGER now_LargeInteger,lsn_oldest_LargeInteger,lsn_newest_LargeInteger;
788 page=captive_shared_cache_map_object->pages+now/PAGE_SIZE;
789 if (!page->data_valid)
793 if (page->lsn_oldest && (!param->result || param->result>page->lsn_oldest))
794 param->result=page->lsn_oldest;
796 now_LargeInteger.QuadPart=now;
797 lsn_oldest_LargeInteger.QuadPart=page->lsn_oldest;
798 lsn_newest_LargeInteger.QuadPart=page->lsn_newest;
799 (*param->DirtyPageRoutine)(
800 captive_shared_cache_map_object->FileObject, /* FileObject */
801 &now_LargeInteger, /* FileOffset */
802 PAGE_SIZE, /* Length */
803 &lsn_oldest_LargeInteger, /* OldestLsn */
804 &lsn_newest_LargeInteger, /* NewestLsn */
805 param->Context1, /* Context1 */
806 param->Context2); /* Context2 */
810 gint64 captive_shared_cache_map_CcGetDirtyPages(PDIRTY_PAGE_ROUTINE DirtyPageRoutine,VOID *Context1,VOID *Context2)
812 captive_shared_cache_map_CcGetDirtyPages_foreach_param param;
814 g_return_val_if_fail(DirtyPageRoutine!=NULL,0);
816 param.DirtyPageRoutine=DirtyPageRoutine;
817 param.Context1=Context1;
818 param.Context2=Context2;
821 CaptiveSharedCacheMapObject_hash_init();
822 g_hash_table_foreach(
823 CaptiveSharedCacheMapObject_hash, /* hash_table */
824 (GHFunc)captive_shared_cache_map_CcGetDirtyPages_foreach, /* func */
825 ¶m); /* user_data */
830 gpointer captive_shared_cache_map_get_buffer(CaptiveSharedCacheMapObject *captive_shared_cache_map_object)
832 g_return_val_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object),NULL);
833 g_return_val_if_fail(captive_shared_cache_map_object->buffer!=NULL,NULL);
835 return captive_shared_cache_map_object->buffer;
838 static void captive_cc_FileObject_delete_foreach(
839 CaptiveSharedCacheMapObject *captive_shared_cache_map_object, /* key */
840 CaptiveSharedCacheMapObject *captive_shared_cache_map_object_value, /* value */
841 FILE_OBJECT *FileObject) /* user_data */
843 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
844 g_return_if_fail(captive_shared_cache_map_object==captive_shared_cache_map_object_value);
845 g_return_if_fail(FileObject!=NULL);
847 g_assert(FileObject!=captive_shared_cache_map_object->FileObject);
850 BOOLEAN captive_cc_FileObject_delete(FILE_OBJECT *FileObject)
852 g_return_val_if_fail(FileObject!=NULL,FALSE);
854 CaptiveSharedCacheMapObject_hash_init();
855 g_hash_table_foreach(
856 CaptiveSharedCacheMapObject_hash, /* hash_table */
857 (GHFunc)captive_cc_FileObject_delete_foreach, /* func */
858 FileObject); /* user_data */
860 return FALSE; /* FIXME: remove useless return code. */