739ebea700783bd29655fcebed315a4eace5af87
[captive.git] / src / libcaptive / cc / bcbpin.c
1 /* $Id$
2  * reactos Cache Manager (Cc*) pin Bcb handling of libcaptive
3  * Copyright (C) 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 "privatebcbpin.h"
23 #include "reactos/ddk/ccfuncs.h"
24 #include "privatebcb.h"
25 #include "captive/macros.h"
26
27
28 /**
29  * CcPinMappedData:
30  * @FileObject: Initialized open #FileObject to map.
31  * %NULL value is forbidden.
32  * @MappedFileOffset: The @FileObject file offset from where to map the region from.
33  * Negative value is forbidden.
34  * @MappedLength: Requested length of the region to map from @FileObject.
35  * FIXME: Value %0 is currently forbidden by libcaptive; it should be allowed.
36  * @Wait: Whether disk waiting is permitted for this function.
37  * Value currently ignored by libcaptive as the data must have been mapped by CcMapData() already anyway.
38  * @Flags: %PIN_WAIT means whether disk waiting is permitted for this function.
39  * Value without %PIN_WAIT is currently permtted by libcaptive as the data must have been mapped by CcMapData() already anyway.
40  * %PIN_NO_READ is the same as %MAP_NO_READ - see CcMapData().
41  * FIXME: %PIN_EXCLUSIVE for exclusive @Bcb access is now ignored by libcaptive.
42  * %PIN_IF_BCB if @Bcb should never be created; function is successful only if @Bcb already exists.
43  * @Bcb: Returns initialized #PUBLIC_BCB to refer to the mapped region.
44  * The memory region can be larger than requested as it is %PAGE_SIZE aligned.
45  * %NULL pointer is forbidden.
46  * @Buffer: Returns the mapped memory region start address.
47  * This address may not be %PAGE_SIZE aligned.
48  * %NULL pointer is forbidden.
49  *
50  * This function will allow you to modify the data mapped by CcMapData().
51  * libcaptive does not differentiate this function with CcMapData().
52  *
53  * This call does not set the buffer as dirty - such buffer will not be flushed automatically.
54  *
55  * NEVER re-read any memory from FileObject here!
56  * at least fastfat.sys directory create relies on the fact of CcPinRead()
57  * with already modified buffers to be left intact.
58  *
59  * This call will proceed as CcPinRead() if such #Bcb does not yet exist.
60  * This is IMO just a bug workaround for a peruse by fastfat.sys FatLocateVolumeLabel().
61  *
62  * Every call to this function must be matched by a one corresponding CcUnpinData() call.
63  *
64  * Returns: %TRUE if the region was successfuly mapped.
65  * @Bcb with the initialized new memory region.
66  * @Buffer with the address of the exact byte specified by @FileOffset.
67  */
68 BOOLEAN CcPinMappedData
69                 (IN PFILE_OBJECT FileObject,IN PLARGE_INTEGER FileOffset,IN ULONG Length,IN ULONG Flags,OUT PVOID *Bcb)
70 {
71 CaptiveSharedCacheMapObject *SharedCacheMap;
72 CaptivePrivateBcbPinObject *captive_private_bcb_pin_object;
73
74         g_return_val_if_fail(FileObject!=NULL,FALSE);
75         g_return_val_if_fail(FileOffset!=NULL,FALSE);
76         g_return_val_if_fail(FileOffset->QuadPart>=0,FALSE);
77         g_return_val_if_fail(Length>0,FALSE);   /* FIXME: not handled below; 0 should be allowed */
78         g_return_val_if_fail(Flags==PIN_WAIT,FALSE);    /* FIXME */
79         g_return_val_if_fail(Bcb!=NULL,FALSE);
80
81         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: FileObject=%p,FileOffset=0x%llX,Length=0x%lX,Flags=0x%lX",G_STRLOC,
82                         FileObject,(guint64)FileOffset->QuadPart,(gulong)Length,(gulong)Flags);
83
84         g_assert(Length<=PAGE_SIZE);
85         /* Check PAGE_SIZE crossing */
86         g_assert((FileOffset->QuadPart&~(PAGE_SIZE-1))==((FileOffset->QuadPart+Length-1)&~(PAGE_SIZE-1)));
87
88         SharedCacheMap=captive_FileObject_to_SharedCacheMap(FileObject);
89         captive_private_bcb_pin_object=captive_private_bcb_pin_object_get_ref(SharedCacheMap,FileOffset->QuadPart);
90
91         captive_shared_cache_map_data_validate_noread(SharedCacheMap,FileOffset->QuadPart,FileOffset->QuadPart+Length);
92
93         *Bcb=captive_private_bcb_object_get_PublicBcb(CAPTIVE_PRIVATE_BCB_OBJECT(captive_private_bcb_pin_object));
94
95         return TRUE;
96 }
97
98
99 /**
100  * CcPinRead:
101  * @FileObject: Initialized open #FileObject to map.
102  * %NULL value is forbidden.
103  * @FileOffset: The @FileObject file offset from where to map the region from.
104  * Negative value is forbidden.
105  * @Length: Requested length of the region to map from @FileObject.
106  * FIXME: Value %0 is currently forbidden by libcaptive; it should be allowed.
107  * @Flags: %PIN_WAIT means whether disk waiting is permitted for this function.
108  * Value without %PIN_WAIT is currently permtted by libcaptive as the data must have been mapped by CcMapData() already anyway.
109  * %PIN_NO_READ is the same as %MAP_NO_READ - see CcMapData().
110  * FIXME: %PIN_EXCLUSIVE for exclusive @Bcb access is now ignored by libcaptive.
111  * %PIN_IF_BCB if @Bcb should never be created; function is successful only if @Bcb already exists.
112  * @Bcb: Returns initialized #PUBLIC_BCB to refer to the mapped region.
113  * The memory region can be larger than requested as it is %PAGE_SIZE aligned.
114  * %NULL pointer is forbidden.
115  * @Buffer: Returns the mapped memory region start address.
116  * This address may not be %PAGE_SIZE aligned.
117  * %NULL pointer is forbidden.
118  *
119  * Merely a shortcut call for CcMapData() and CcPinMappedData() afterwards.
120  * See these two functions for the details. It has a difference to subsequent
121  * calling of CcMapData() and CcPinMappedData() instead as this call counts
122  * only as one function for a corresponding CcUnpinData() call.
123  *
124  * This call does not set the buffer as dirty - such buffer will not be flushed automatically.
125  *
126  * Every call to this function must be matched by a one corresponding CcUnpinData() call.
127  *
128  * Returns: %TRUE if the region was successfuly mapped.
129  * @Bcb with the initialized new memory region.
130  * @Buffer with the address of the exact byte specified by @FileOffset.
131  */
132 BOOLEAN CcPinRead(IN PFILE_OBJECT FileObject,
133                 IN PLARGE_INTEGER FileOffset,IN ULONG Length,IN ULONG Flags,OUT PVOID *Bcb,OUT PVOID *Buffer)
134 {
135 CaptiveSharedCacheMapObject *SharedCacheMap;
136 CaptivePrivateBcbPinObject *captive_private_bcb_pin_object;
137
138         g_return_val_if_fail(FileObject!=NULL,FALSE);
139         g_return_val_if_fail(FileOffset!=NULL,FALSE);
140         g_return_val_if_fail(FileOffset->QuadPart>=0,FALSE);
141         g_return_val_if_fail(Length>0,FALSE);   /* FIXME: not handled below; 0 should be allowed */
142         g_return_val_if_fail(Flags==PIN_WAIT,FALSE);    /* FIXME */
143         g_return_val_if_fail(Bcb!=NULL,FALSE);
144         g_return_val_if_fail(Buffer!=NULL,FALSE);
145
146         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: FileObject=%p,FileOffset=0x%llX,Length=0x%lX,Flags=0x%lX",G_STRLOC,
147                         FileObject,(guint64)FileOffset->QuadPart,(gulong)Length,(gulong)Flags);
148
149         g_assert(Length<=PAGE_SIZE);
150         /* Check PAGE_SIZE crossing */
151         g_assert((FileOffset->QuadPart&~(PAGE_SIZE-1))==((FileOffset->QuadPart+Length-1)&~(PAGE_SIZE-1)));
152
153         SharedCacheMap=captive_FileObject_to_SharedCacheMap(FileObject);
154         captive_private_bcb_pin_object=captive_private_bcb_pin_object_get_ref(SharedCacheMap,FileOffset->QuadPart);
155
156         captive_shared_cache_map_data_validate_read(SharedCacheMap,FileObject,FileOffset->QuadPart,FileOffset->QuadPart+Length);
157
158         *Bcb=captive_private_bcb_object_get_PublicBcb(CAPTIVE_PRIVATE_BCB_OBJECT(captive_private_bcb_pin_object));
159         *Buffer=captive_shared_cache_map_get_buffer(SharedCacheMap)+FileOffset->QuadPart;
160
161         return TRUE;
162 }
163
164
165 /**
166  * CcSetDirtyPinnedData:
167  * @Bcb: #PUBLIB_BCB to be unpinned from CcRepinBcb().
168  * %NULL value is forbidden.
169  * @Lsn: Optional LSN (Linear Sequence Number) to assign to @Bcb.
170  * %NULL pointer is permitted.
171  *
172  * This call will set the buffer as dirty - such buffer will be flushed automatically.
173  *
174  * You should call it only on CcPin*() buffers - not just CcMapData() buffers
175  * although libcaptive does not differentiate it.
176  */
177 VOID CcSetDirtyPinnedData(IN PVOID Bcb,IN PLARGE_INTEGER Lsn OPTIONAL)
178 {
179 CaptivePrivateBcbPinObject *captive_private_bcb_pin_object;
180
181         g_return_if_fail(Bcb!=NULL);
182
183         captive_private_bcb_pin_object=CAPTIVE_PRIVATE_BCB_PIN_OBJECT(captive_PublicBcb_to_PrivateBcbObject(Bcb));
184
185         captive_private_bcb_pin_object_set_dirty(captive_private_bcb_pin_object);
186         if (Lsn)
187                 captive_private_bcb_pin_object_set_lsn(captive_private_bcb_pin_object,Lsn->QuadPart);
188 }
189
190
191 /**
192  * CcPreparePinWrite:
193  * @FileObject: Initialized open #FileObject to map.
194  * %NULL value is forbidden.
195  * @FileOffset: The @FileObject file offset from where to map the region from.
196  * Negative value is forbidden.
197  * @Length: Requested length of the region to map from @FileObject.
198  * FIXME: Value %0 is currently forbidden by libcaptive; it should be allowed.
199  * @Zero: %TRUE if the area of @FileOffset...@FileOffset+@Length should be cleared.
200  * @Flags: %PIN_WAIT means whether disk waiting is permitted for this function.
201  * Value without %PIN_WAIT is currently permtted by libcaptive as the data must have been mapped by CcMapData() already anyway.
202  * %PIN_NO_READ is the same as %MAP_NO_READ - see CcMapData().
203  * FIXME: %PIN_EXCLUSIVE for exclusive @Bcb access is now ignored by libcaptive.
204  * %PIN_IF_BCB if @Bcb should never be created; function is successful only if @Bcb already exists.
205  * @Bcb: Returns initialized #PUBLIC_BCB to refer to the mapped region.
206  * The memory region can be larger than requested as it is %PAGE_SIZE aligned.
207  * %NULL pointer is forbidden.
208  * @Buffer: Returns the mapped memory region start address.
209  * This address may not be %PAGE_SIZE aligned.
210  * %NULL pointer is forbidden.
211  *
212  * Wrapper for a pair of CcPinRead() and CcSetDirtyPinnedData().
213  * The mapped range can be also optionally cleared if @Zero is specified.
214  * See CcPinRead() for a more detailed documentation.
215  *
216  * This call will set the buffer as dirty - such buffer will be flushed automatically.
217  *
218  * Returns: %TRUE if the mapping was successful.
219  */
220 BOOLEAN CcPreparePinWrite(IN PFILE_OBJECT FileObject,
221                 IN PLARGE_INTEGER FileOffset,IN ULONG Length,IN BOOLEAN Zero,IN ULONG Flags,OUT PVOID *Bcb,OUT PVOID *Buffer)
222 {
223 CaptiveSharedCacheMapObject *SharedCacheMap;
224 CaptivePrivateBcbPinObject *captive_private_bcb_pin_object;
225
226         g_return_val_if_fail(FileObject!=NULL,FALSE);
227         g_return_val_if_fail(FileOffset!=NULL,FALSE);
228         g_return_val_if_fail(FileOffset->QuadPart>=0,FALSE);
229         g_return_val_if_fail(Length>0,FALSE);   /* FIXME: not handled below; 0 should be allowed */
230         g_return_val_if_fail(Flags==PIN_WAIT,FALSE);    /* FIXME */
231         g_return_val_if_fail(Bcb!=NULL,FALSE);
232         g_return_val_if_fail(Buffer!=NULL,FALSE);
233
234         g_assert(0==CAPTIVE_ROUND_DOWN_EXCEEDING64(FileOffset->QuadPart,PAGE_SIZE));    /* NOT YET IMPLEMENTED */
235         g_assert(Length==PAGE_SIZE);    /* NOT YET IMPLEMENTED */
236
237         SharedCacheMap=captive_FileObject_to_SharedCacheMap(FileObject);
238         captive_private_bcb_pin_object=captive_private_bcb_pin_object_get_ref(SharedCacheMap,FileOffset->QuadPart);
239
240         captive_shared_cache_map_set_data_valid(SharedCacheMap,FileOffset->QuadPart,FileOffset->QuadPart+Length);
241         if (Zero)
242                 memset(captive_shared_cache_map_get_buffer(SharedCacheMap)+FileOffset->QuadPart,0,Length);
243         captive_shared_cache_map_set_dirty(SharedCacheMap,FileOffset->QuadPart,FileOffset->QuadPart+Length);
244
245         *Bcb=captive_private_bcb_object_get_PublicBcb(CAPTIVE_PRIVATE_BCB_OBJECT(captive_private_bcb_pin_object));
246         *Buffer=captive_shared_cache_map_get_buffer(SharedCacheMap)+FileOffset->QuadPart;
247
248         return TRUE;
249 }