cdc5a0cb69bdb6633b1d2ea20b276a7ce6aa6d4a
[captive.git] / src / libcaptive / cc / init.c
1 /* $Id$
2  * reactos Cache Manager initialization functions of libcaptive
3  * Copyright (C) 2002-2003 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
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.
13  * 
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
17  */
18
19
20 #include "config.h"
21
22 #include <glib/gmessages.h>
23 #include "reactos/ntos/types.h"
24 #include "reactos/ddk/iotypes.h"
25 #include "reactos/ddk/cctypes.h"
26 #include "sharedcachemap.h"
27 #include "reactos/ddk/kefuncs.h"
28
29
30 /**
31  * CcInitializeCacheMap:
32  * @FileObject: Existing file to set callbacks for. Ignored by libcaptive.
33  * %NULL value is forbidden.
34  * @FileSizes: Some file sizes suggestions. Ignored by libcaptive.
35  * %NULL value is forbidden.
36  * @PinAccess: CcPin*() functions will be used with @FileObject? Ignored by libcaptive.
37  * @CallBacks: Provided callback functions for readahead/writebehind. Ignored by libcaptive.
38  * %NULL value is forbidden.
39  * @LazyWriterContext: Value passed to functions of @CallBacks to bind with @FileObject.
40  * %NULL value is permitted.
41  *
42  * Provides support of readahead/writebehind in W32. Function should be called
43  * by W32 filesystem to offer its functions to W32 kernel. These functions
44  * are never called by libcaptive, this call is a NOP in libcaptive.
45  *
46  * VERIFIED: Double CcInitializeCacheMap() without CcUninitializeCacheMap().
47  */
48 VOID CcInitializeCacheMap(IN PFILE_OBJECT FileObject,
49                 IN PCC_FILE_SIZES FileSizes,IN BOOLEAN PinAccess,IN PCACHE_MANAGER_CALLBACKS CallBacks,IN PVOID LazyWriterContext)
50 {
51 CaptiveSharedCacheMapObject *SharedCacheMap;
52
53         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"enter: CcInitializeCacheMap: FileObject=0x%lX,"
54                                         "FileSizes,->AllocationSize=0x%lX,->FileSize=0x%lX,->ValidDataLength=0x%lX,"
55                                         "PinAccess=%d,Callbacks,LazyWriteContext",
56                         (long)FileObject,
57                                         (!FileSizes ? -1 : (long)FileSizes->AllocationSize.QuadPart),
58                                         (!FileSizes ? -1 : (long)FileSizes->FileSize.QuadPart),
59                                         (!FileSizes ? -1 : (long)FileSizes->ValidDataLength.QuadPart),
60                                         PinAccess);
61
62         g_return_if_fail(FileObject!=NULL);
63         g_return_if_fail(FileSizes!=NULL);
64         g_return_if_fail(CallBacks!=NULL);
65
66         /* VERIFIED: CcInitializeCacheMap() has already 'SectionObjectPointers' allocated. */
67         g_return_if_fail(FileObject->SectionObjectPointer!=NULL);
68
69         SharedCacheMap=captive_shared_cache_map_get_ref(FileObject,FileSizes,PinAccess,CallBacks,LazyWriterContext);
70
71         captive_shared_cache_map_w32_ref(SharedCacheMap);
72         g_object_unref(SharedCacheMap); /* sink */
73
74         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"leave: CcInitializeCacheMap");
75 }
76
77
78 /**
79  * CcUninitializeCacheMap:
80  * @FileObject: File to close caching for.
81  * %NULL value is forbidden.
82  * @TruncateSize: Current file size if @FileObject was truncated to it in the meantime.
83  * @UninitializeCompleteEvent: Optional event to signal after physical write to disk.
84  * %NULL value is permitted.
85  *
86  * Close the cachine facilities from CcInitializeCacheMap().
87  * It is valid to pass @FileObject without being registered by CcInitializeCacheMap().
88  *
89  * FIXME; What to do with files with dirty blocks? Currently we fail assertion on them
90  * although I think W32 would drop such buffers (purge them - not flush them).
91  *
92  * Returns: %TRUE if the caching was closed successfuly.
93  * %FALSE if @FileObject was not registered by CcInitializeCacheMap()
94  * or if its #SharedCacheMap is still in use (such as by other #FileObject reference of
95  * the same #FCB instance).
96  */
97 BOOLEAN CcUninitializeCacheMap(IN PFILE_OBJECT FileObject,
98                 IN PLARGE_INTEGER TruncateSize OPTIONAL,IN PCACHE_UNINITIALIZE_EVENT UninitializeCompleteEvent OPTIONAL)
99 {
100 CaptiveSharedCacheMapObject *SharedCacheMap;
101 BOOLEAN r;
102
103         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"enter: CcUninitializeCacheMap: FileObject=0x%lX,TruncateSize=0x%lX,UninitializeCompleteEvent=0x%lX",
104                         (long)FileObject,(!TruncateSize ? -1 : (long)TruncateSize->QuadPart),(long)UninitializeCompleteEvent);
105
106         g_return_val_if_fail(FileObject!=NULL,FALSE);
107         /* assert current size ==*TruncateSize if TruncateSize */
108
109         if (FileObject->SectionObjectPointer && !FileObject->SectionObjectPointer->SharedCacheMap)
110                 r=FALSE;
111         else {
112                 SharedCacheMap=captive_FileObject_to_SharedCacheMap(FileObject);
113
114                 /* We may be called without preceding CcInitializeCacheMap(). */
115                 if (!captive_shared_cache_map_query_w32_ref(SharedCacheMap))
116                         r=FALSE;
117                 else {
118                         captive_shared_cache_map_w32_unref(SharedCacheMap);
119                         r=(FileObject->SectionObjectPointer->SharedCacheMap==NULL);
120                         }
121
122                 /* FIXME: should we do KePulseEvent? Are we allowed to signal from inside CcUninitializeCacheMap() ? */
123                 if (UninitializeCompleteEvent)
124                         KeSetEvent(
125                                         &UninitializeCompleteEvent->Event,      /* Event */
126                                         IO_NO_INCREMENT,        /* Increment */
127                                         FALSE); /* Wait */
128                 }
129
130         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"leave: CcUninitializeCacheMap: r=%d",r);
131
132         return r;
133 }
134
135
136 /**
137  * CcSetFileSizes:
138  * @FileObject: Initialized open #FileObject to update file sizes of.
139  * %NULL value is forbidden.
140  * @FileSizes: New file sizes to update cache to.
141  * %NULL value is forbidden.
142  * 
143  * Update cache properties after file sizes were updated.
144  * Probably only the exceeding pages need to be unmapped and BCBs updated
145  * if FileSizes->AllocationSize gets shrunk.
146  *
147  * #AllocationSize must not change if any map or pin Bcbs exist.
148  */
149 VOID CcSetFileSizes(IN PFILE_OBJECT FileObject,IN PCC_FILE_SIZES FileSizes)
150 {
151 CaptiveSharedCacheMapObject *SharedCacheMap;
152
153         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"enter: CcSetFileSizes: FileObject=0x%lX,"
154                                         "FileSizes,->AllocationSize=0x%lX,->FileSize=0x%lX,->ValidDataLength=0x%lX",
155                         (long)FileObject,
156                                         (!FileSizes ? -1 : (long)FileSizes->AllocationSize.QuadPart),
157                                         (!FileSizes ? -1 : (long)FileSizes->FileSize.QuadPart),
158                                         (!FileSizes ? -1 : (long)FileSizes->ValidDataLength.QuadPart));
159
160         g_return_if_fail(FileObject!=NULL);
161         g_return_if_fail(FileSizes!=NULL);
162
163         /* Needed by ext2fsd.sys-v0.10a: */
164         if (1
165                         && FileObject->SectionObjectPointer
166                         && FileObject->SectionObjectPointer->SharedCacheMap) {
167                 SharedCacheMap=captive_FileObject_to_SharedCacheMap(FileObject);
168                 captive_shared_cache_map_FileSizes_set(SharedCacheMap,FileSizes);
169                 }
170
171         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"leave: CcSetFileSizes");
172 }