Initial original import from: fuse-2.4.2-2.fc4
[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         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"enter: CcInitializeCacheMap: FileObject=0x%lX,"
52                                         "FileSizes,->AllocationSize=0x%lX,->FileSize=0x%lX,->ValidDataLength=0x%lX,"
53                                         "PinAccess=%d,Callbacks,LazyWriteContext",
54                         (long)FileObject,
55                                         (!FileSizes ? -1 : (long)FileSizes->AllocationSize.QuadPart),
56                                         (!FileSizes ? -1 : (long)FileSizes->FileSize.QuadPart),
57                                         (!FileSizes ? -1 : (long)FileSizes->ValidDataLength.QuadPart),
58                                         PinAccess);
59
60         g_return_if_fail(FileObject!=NULL);
61         g_return_if_fail(FileSizes!=NULL);
62         g_return_if_fail(CallBacks!=NULL);
63
64         /* VERIFIED: CcInitializeCacheMap() has already 'SectionObjectPointers' allocated. */
65         g_return_if_fail(FileObject->SectionObjectPointer!=NULL);
66
67         captive_shared_cache_map_get_ref(FileObject,FileSizes,PinAccess,CallBacks,LazyWriterContext);
68
69         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"leave: CcInitializeCacheMap");
70 }
71
72
73 /**
74  * CcUninitializeCacheMap:
75  * @FileObject: File to close caching for.
76  * %NULL value is forbidden.
77  * @TruncateSize: Current file size if @FileObject was truncated to it in the meantime.
78  * @UninitializeCompleteEvent: Optional event to signal after physical write to disk.
79  * %NULL value is permitted.
80  *
81  * Close the cachine facilities from CcInitializeCacheMap().
82  * It is valid to pass @FileObject without being registered by CcInitializeCacheMap().
83  *
84  * FIXME; What to do with files with dirty blocks? Currently we fail assertion on them
85  * although I think W32 would drop such buffers (purge them - not flush them).
86  *
87  * Returns: %TRUE if the caching was closed successfuly.
88  * %FALSE if @FileObject was not registered by CcInitializeCacheMap()
89  * or if its #SharedCacheMap is still in use (such as by other #FileObject reference of
90  * the same #FCB instance).
91  */
92 BOOLEAN CcUninitializeCacheMap(IN PFILE_OBJECT FileObject,
93                 IN PLARGE_INTEGER TruncateSize OPTIONAL,IN PCACHE_UNINITIALIZE_EVENT UninitializeCompleteEvent OPTIONAL)
94 {
95 CaptiveSharedCacheMapObject *SharedCacheMap;
96 BOOLEAN r;
97
98         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"enter: CcUninitializeCacheMap: FileObject=0x%lX,TruncateSize=0x%lX,UninitializeCompleteEvent=0x%lX",
99                         (long)FileObject,(!TruncateSize ? -1 : (long)TruncateSize->QuadPart),(long)UninitializeCompleteEvent);
100
101         g_return_val_if_fail(FileObject!=NULL,FALSE);
102         /* assert current size ==*TruncateSize if TruncateSize */
103
104         if (FileObject->SectionObjectPointer && !FileObject->SectionObjectPointer->SharedCacheMap)
105                 r=FALSE;
106         else {
107                 SharedCacheMap=captive_FileObject_to_SharedCacheMap(FileObject);
108
109                 /* We may be called without preceding CcInitializeCacheMap(). */
110                 if (!captive_shared_cache_map_query_w32_ref(SharedCacheMap))
111                         r=FALSE;
112                 else {
113                         captive_shared_cache_map_w32_unref(SharedCacheMap);
114                         r=(FileObject->SectionObjectPointer->SharedCacheMap==NULL);
115                         }
116
117                 /* FIXME: should we do KePulseEvent? Are we allowed to signal from inside CcUninitializeCacheMap() ? */
118                 if (UninitializeCompleteEvent)
119                         KeSetEvent(
120                                         &UninitializeCompleteEvent->Event,      /* Event */
121                                         IO_NO_INCREMENT,        /* Increment */
122                                         FALSE); /* Wait */
123                 }
124
125         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"leave: CcUninitializeCacheMap: r=%d",r);
126
127         return r;
128 }
129
130
131 /**
132  * CcSetFileSizes:
133  * @FileObject: Initialized open #FileObject to update file sizes of.
134  * %NULL value is forbidden.
135  * @FileSizes: New file sizes to update cache to.
136  * %NULL value is forbidden.
137  * 
138  * Update cache properties after file sizes were updated.
139  * Probably only the exceeding pages need to be unmapped and BCBs updated
140  * if FileSizes->AllocationSize gets shrunk.
141  *
142  * #AllocationSize must not change if any map or pin Bcbs exist.
143  */
144 VOID CcSetFileSizes(IN PFILE_OBJECT FileObject,IN PCC_FILE_SIZES FileSizes)
145 {
146 CaptiveSharedCacheMapObject *SharedCacheMap;
147
148         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"enter: CcSetFileSizes: FileObject=0x%lX,"
149                                         "FileSizes,->AllocationSize=0x%lX,->FileSize=0x%lX,->ValidDataLength=0x%lX",
150                         (long)FileObject,
151                                         (!FileSizes ? -1 : (long)FileSizes->AllocationSize.QuadPart),
152                                         (!FileSizes ? -1 : (long)FileSizes->FileSize.QuadPart),
153                                         (!FileSizes ? -1 : (long)FileSizes->ValidDataLength.QuadPart));
154
155         g_return_if_fail(FileObject!=NULL);
156         g_return_if_fail(FileSizes!=NULL);
157
158         /* Needed by ext2fsd.sys-v0.10a: */
159         if (1
160                         && FileObject->SectionObjectPointer
161                         && FileObject->SectionObjectPointer->SharedCacheMap) {
162                 SharedCacheMap=captive_FileObject_to_SharedCacheMap(FileObject);
163                 captive_shared_cache_map_FileSizes_set(SharedCacheMap,FileSizes);
164                 }
165
166         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"leave: CcSetFileSizes");
167 }