+Workaround Linux kernel last device block inaccessibility.
[captive.git] / src / libcaptive / client / init.c
1 /* $Id$
2  * Init and cleanup code of libcaptive to be called by client application
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 "init.h"       /* self */
23 #include "captive/ldr.h"
24 #include "captive/ldr_exports.h"
25 #include "captive/unicode.h"
26 #include "captive/rtl-file.h"
27 #include <glib/gtypes.h>
28 #include <glib/gmessages.h>
29 #include "reactos/internal/ldr.h"
30 #include "reactos/napi/types.h"
31 #include "reactos/internal/kd.h"        /* for KDB_LOADDRIVER_HOOK */
32 #include <fcntl.h>
33 #include <sys/mman.h>   /* for PROT_READ, MAP_SHARED */
34 #include "reactos/ddk/kefuncs.h"        /* for KeInitializeSpinLock() */
35 #include "reactos/internal/ntoskrnl.h"  /* for IoInit() */
36 #include "reactos/internal/ps.h"        /* for PsInitProcessManagment() and PsInitThreadManagment() */
37 #include "reactos/ddk/iofuncs.h"        /* for IoCreateFile() */
38 #include "captive/storage.h"
39 #include "captive/signal.h"     /* for captive_signal_init() */
40 #include "reactos/ddk/psfuncs.h"        /* for PsGetCurrentThread() */
41 #include <stdio.h>
42 #include <popt.h>
43 #include <glib/gstrfuncs.h>
44 #include <glib/glist.h>
45 #include "giochannel-blind.h"
46 #include <glib-object.h>
47 #include "reactos/internal/se.h"        /* for SeInit2() */
48 #include "captive/leave.h"
49 #include "captive/options.h"
50 #include <libgnomevfs/gnome-vfs-result.h>
51 #include "lib.h"
52 #include <reactos/ddk/obfuncs.h>
53 #include <syslog.h>
54 #include "captive/macros.h"
55 #include "../storage/relastblock.h"     /* for captive_storage_relastblock() */
56
57
58 struct captive_options *captive_options;
59
60 /* Are we initialized? */
61 static gboolean active;
62
63 /* Module of fs module itself loaded by captive_w32_init() */
64 static PMODULE_OBJECT ModuleObject;
65
66 /* Driver in fs module loaded by captive_w32_init() */
67 DRIVER_OBJECT captive_DriverObject;
68 PDRIVER_REINITIALIZE captive_DriverObject_ReinitRoutine;
69 PVOID captive_DriverObject_ReinitRoutine_Context;
70
71 /* Structure holding the pointer to the toplevel IRP */
72 static TOP_LEVEL_IRP TopLevelIrp;       /* TODO:thread */
73
74
75 void *_local_unwind2_addr;
76
77
78 gboolean captive_debug_messages_disabled=FALSE;
79
80 static gboolean captive_w32_init(void)
81 {
82 NTSTATUS err;
83 gboolean errbool;
84
85         g_return_val_if_fail(captive_options!=NULL,FALSE);
86         g_return_val_if_fail(captive_options->filesystem.type!=CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY,FALSE);
87
88         /* captive_giochannel_size() only _after_ g_io_channel_set_encoding() ! */
89         captive_image_size=captive_giochannel_size(captive_image_iochannel);
90         g_return_val_if_fail(captive_image_size>0,FALSE);
91
92         /* Part of reactos/ntoskrnl/ke/main.c/KiSystemStartup() begins. */
93         /* ExpInitializeExecutive(); */
94                 /* Part of reactos/ntoskrnl/ke/main.c/ExpInitializeExecutive() begins
95                  * here as the rest of the function does a lot of hardware initializations.
96                  */
97                 /* LdrInit1(); */
98                         /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInit1() begins. */
99                         InitializeListHead(&ModuleTextListHead);
100                         /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInit1() ends. */
101                 KeLowerIrql(DISPATCH_LEVEL);
102                 /*...*/
103                 /* FIXME: create default nls tables? Really still needed? */
104                 /* Obsolete: RtlpInitNlsTables(); */
105                 /*...*/
106                 /* KeInit2() */
107                         /*...*/
108                         KeInitializeDispatcher();
109                         /*...*/
110                 KeLowerIrql(PASSIVE_LEVEL);
111                 errbool=SeInit1();
112                 g_assert(errbool==TRUE);
113                 ObInit();
114                 errbool=SeInit2();
115                 g_assert(errbool==TRUE);
116                 /* PiInitProcessManager(); */
117                         /* Part of reactos/ntoskrnl/ps/psmgr.c/PiInitProcessManager() begins. */
118                         PsInitProcessManagment();
119                         PsInitThreadManagment();
120                         /* Part of reactos/ntoskrnl/ps/psmgr.c/PiInitProcessManager() ends. */
121                 /*...*/
122                 IoInit();
123                 /*...*/
124                 /* LdrInitModuleManagement(); */
125                         /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInitModuleManagement() begins
126                          * here as the rest "Create module object for {NTOSKRNL,HAL}"
127                          * is dependent on {NTOSKRNL,HAL} PE image headers not provided by libcaptive.
128                          */
129                         /* Initialize the module list and spinlock */
130                         InitializeListHead(&ModuleListHead);
131                         KeInitializeSpinLock(&ModuleListLock);
132                         /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInitModuleManagement ends. */
133                 /*...*/
134                 /* Ntinit(); */
135                         NtInitializeEventImplementation();
136                         /*...*/
137                 /* Part of reactos/ntoskrnl/ke/main.c/ExpInitializeExecutive() ends. */
138         /* Part of reactos/ntoskrnl/ke/main.c/KiSystemStartup() ends. */
139
140         /* Simulate our PE headers and export the symbols of our complete libraries */
141         captive_kernel_exports();
142
143         errbool=captive_cdrom_init();
144         g_return_val_if_fail(errbool==TRUE,FALSE);
145         errbool=captive_disk_init();
146         g_return_val_if_fail(errbool==TRUE,FALSE);
147
148         while (captive_options->load_module) {
149 PMODULE_OBJECT ModuleObject_tmp;
150 NTSTATUS err;
151
152                 /* load the module */
153                 err=captive_LdrLoadModule(
154                                 captive_options->load_module->data,
155                                 &ModuleObject_tmp);     /* ModuleObjectp */
156                 g_return_val_if_fail(NT_SUCCESS(err),FALSE);
157
158                 captive_options_module_free(captive_options->load_module->data);
159                 /* also frees 'options->load_module->data' */
160                 captive_options->load_module=g_list_delete_link(captive_options->load_module,captive_options->load_module);
161                 }
162
163         /* Patch 'ntoskrnl.exe' loaded by 'captive_options->load_module' above. */
164         {
165         CHAR *KeNumberProcessorsp=captive_Module_GetExportAddress("ntoskrnl.exe","KeNumberProcessors");
166         
167                 g_assert(*KeNumberProcessorsp==0);
168                 *KeNumberProcessorsp=KeNumberProcessors;
169                 g_assert(*KeNumberProcessorsp==1);
170                 }
171         /* Apply AFTER any symbols sanity checks above! */
172         if (captive_options->debug_messages)
173                 captive_kernel_patches_debug();
174         else
175                 captive_kernel_patches_nondebug();
176
177         _local_unwind2_addr=captive_Module_GetExportAddress("ntoskrnl.exe","_local_unwind2");
178
179         /* Initialize 'FsRtlLegalAnsiCharacterArray'.
180          * It requires 'ntoskrnl.exe' loaded by 'captive_options->load_module' above;
181          * captive_kernel_patches_debug()/captive_kernel_patches_nondebug() should not be needed.
182          */
183         captive_FsRtlLegalAnsiCharacterArray_init();
184
185         /* set TopLevelIrp() - FIXME: where is it set by native reactos? */
186         PsGetCurrentThread()->TopLevelIrp=&TopLevelIrp; /* otherwise Io{Get,Set}TopLevelIrp() would SIGSEGV */
187
188         /* Begin possible handling of foreign W32 binary code here */
189         /* If you want to disable SIGSEGV handler if not needed:
190          *      if (ModuleObject->Flags & MODULE_FLAG_PE)
191          */
192         captive_signal_init();
193
194         /* You must have already captive_signal_init() passed here as the module may
195          * call some functions from W32 ntoskrnl.exe.
196          */
197         captive_DriverObject_ReinitRoutine=NULL;
198         err=captive_LdrpLoadAndCallImage(
199                         &ModuleObject,  /* ModuleObjectp */
200                         &captive_options->filesystem,   /* options_module */
201                         &captive_DriverObject,  /* DriverEntry_DriverObject */
202                         captive_utf8_to_UnicodeString_alloca("\\captive\\filesystem")); /* DriverEntry_RegistryPath */
203         g_return_val_if_fail(NT_SUCCESS(err),FALSE);
204         if (captive_DriverObject_ReinitRoutine) {
205                 captive_stdcall_call_12((CaptiveStdCallFunc12)captive_DriverObject_ReinitRoutine,
206                                 &captive_DriverObject,  /* DriverObject */
207                                 captive_DriverObject_ReinitRoutine_Context,     /* Context */
208                                 (gpointer)1);   /* Count: # of calls of ReinitRoutine incl. the current one */
209                 }
210
211         return TRUE;
212 }
213
214
215 static void     captive_log_init_g_log_func_discard
216                 (const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,gpointer user_data)
217 {
218         g_return_if_fail(message!=NULL);
219
220         /* NOP */
221 }
222
223 static void captive_log_init_g_log_func
224                 (const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,gpointer user_data /* unused */)
225 {
226 int priority;
227
228         g_return_if_fail(message!=NULL);
229
230                 /* unused: LOG_EMERG */
231                 /* unused: LOG_ALERT */
232         /**/ if (log_level&G_LOG_LEVEL_ERROR)
233                 priority=LOG_CRIT;
234         else if (log_level&G_LOG_LEVEL_CRITICAL)
235                 priority=LOG_ERR;
236         else if (log_level&G_LOG_LEVEL_WARNING)
237                 priority=LOG_WARNING;
238         else if (log_level&G_LOG_LEVEL_MESSAGE)
239                 priority=LOG_NOTICE;
240         else if (log_level&G_LOG_LEVEL_INFO)
241                 priority=LOG_INFO;
242         else if (log_level&G_LOG_LEVEL_DEBUG)
243                 priority=LOG_DEBUG;
244         else /* bogus? */
245                 priority=LOG_WARNING;
246
247         syslog(priority,"%s%s%s",
248                         (!(log_level&G_LOG_FLAG_RECURSION) ? "" : "RECURSION: "),
249                         (!(log_level&G_LOG_FLAG_FATAL    ) ? "" : "FATAL: "),
250                         message);
251 }
252
253 void captive_log_init(const struct captive_options *captive_options)
254 {
255         g_return_if_fail(captive_options!=NULL);
256
257         /* FIXME: Fix sharing of different 'debug_messages' for various sandboxes. */
258         captive_debug_messages_disabled=!captive_options->debug_messages;
259
260         /* FIXME: Fix sharing of different 'debug_messages' for various sandboxes. */
261         if (!captive_options->debug_messages) {
262                 /* FIXME: Save handler_id and destroy it in captive_vfs_close(). */
263                 g_log_set_handler(
264                                 G_LOG_DOMAIN,   /* log_domain; "Captive" */
265                                 0       /* log_levels */
266                                                 | G_LOG_FLAG_RECURSION
267                                                 | G_LOG_FLAG_FATAL
268                                                 /* The same mask is in:
269                                                  * libcaptive/sandbox/server-GLogFunc.c
270                                                  * libcaptive/client/init.c
271                                                  */
272                                                 | G_LOG_LEVEL_MESSAGE
273                                                 | G_LOG_LEVEL_INFO
274                                                 | G_LOG_LEVEL_DEBUG,
275                                 captive_log_init_g_log_func_discard,    /* log_func */
276                                 NULL);  /* user_data */
277                 }
278         /* We are not the sandboxed slave;
279          * 'syslog_facility' would be '-1' in slave anyway as it is not transferred through CORBA.
280          */
281         if (!captive_options->sandbox || (captive_options->sandbox_server_argv || captive_options->sandbox_server_ior)) {
282                 if (captive_options->syslog_facility!=-1) {
283                         openlog(
284                                         /* FIXME: Prefix 'ident' by device/mountpoint. */
285                                         G_LOG_DOMAIN,   /* ident; "Captive"; FIXME: lowercase it for syslog(3)? */
286                                         LOG_CONS|LOG_PID,       /* options */
287                                         captive_options->syslog_facility);      /* facility */
288                         /* FIXME: Save handler_id and destroy it in captive_vfs_close(). */
289                         g_log_set_handler(
290                                         G_LOG_DOMAIN,   /* log_domain; "Captive" */
291                                         0       /* log_levels */
292                                                         | 0     /* !G_LOG_FLAG_RECURSION */
293                                                         | G_LOG_FLAG_FATAL
294                                                         | (G_LOG_LEVEL_MASK & ~(captive_options->debug_messages ? 0 : 0
295                                                                         /* The same mask is in:
296                                                                          * libcaptive/sandbox/server-GLogFunc.c
297                                                                          * libcaptive/client/init.c
298                                                                          */
299                                                                         | G_LOG_LEVEL_MESSAGE
300                                                                         | G_LOG_LEVEL_INFO
301                                                                         | G_LOG_LEVEL_DEBUG)),
302                                         (GLogFunc)captive_log_init_g_log_func,  /* log_func */
303                                         NULL);  /* user_data */
304                         }
305                 }
306 }
307
308 /**
309  * captive_init:
310  *
311  * Expects #captive_options: Parsed by captive_options_parse().
312  * %NULL value is forbidden. Field #image_iochannel %NULL value is forbidden.
313  *
314  * Initializes %libcaptive and loads the specified filesystem.
315  *
316  * Returns: %TRUE if successfuly initialized.
317  */
318 gboolean captive_init(void)
319 {
320 gboolean errbool;
321
322         /* We are in sandbox child and we have the right to fail. */
323         g_log_set_always_fatal(~(0
324                         |G_LOG_LEVEL_MESSAGE
325                         |G_LOG_LEVEL_INFO
326                         |G_LOG_LEVEL_DEBUG
327                         ));
328
329         g_return_val_if_fail(active==FALSE,FALSE);
330
331         g_return_val_if_fail(captive_options!=NULL,FALSE);
332         g_return_val_if_fail(captive_options->image_iochannel!=NULL,FALSE);
333
334         /* Initialize GObject subsystem of GLib. */
335         g_type_init();
336
337         captive_log_init(captive_options);
338
339         captive_image_iochannel=captive_options->image_iochannel;
340         g_io_channel_ref(captive_image_iochannel);
341
342         captive_image_iochannel=captive_storage_relastblock(captive_image_iochannel);
343
344         if (captive_options->rwmode==CAPTIVE_OPTION_RWMODE_BLIND) {
345                 GIOChannel *captive_image_iochannel_orig;
346
347                 captive_image_iochannel_orig=captive_image_iochannel;
348                 captive_image_iochannel=(GIOChannel *)captive_giochannel_blind_new(captive_image_iochannel,
349                                 TRUE);  /* writeable */
350                 g_io_channel_unref(captive_image_iochannel_orig);       /* reffed by captive_giochannel_blind_new() */
351                 }
352
353         /* Do not initialize 'captive_image_size' by captive_giochannel_size() here
354          * as we yet need to do g_io_channel_set_encoding().
355          */
356
357         errbool=captive_w32_init();
358         g_return_val_if_fail(errbool==TRUE,FALSE);
359
360         active=TRUE;
361         return TRUE;
362 }
363
364
365 static void dismount_volume(void)
366 {
367 IO_STATUS_BLOCK IoStatusBlock;
368 PIO_STACK_LOCATION StackPtr;
369 PIRP Irp;
370 NTSTATUS Status;
371 DEVICE_OBJECT *DeviceObject=captive_DriverObject.DeviceObject;
372 OBJECT_ATTRIBUTES dir_ObjectAttributes;
373 HANDLE dir_Handle;
374 FILE_OBJECT *FileObject;
375 GnomeVFSResult errvfsresult;
376 NTSTATUS err;
377 IO_STATUS_BLOCK dir_IoStatusBlock;
378 #if 0
379 static const ULONG fsctls[2]={ FSCTL_LOCK_VOLUME,FSCTL_DISMOUNT_VOLUME };
380 int fsctlsi;
381 #endif
382 WCHAR wzero;
383
384         errvfsresult=captive_ObjectAttributes_init("/!Captive!del",&dir_ObjectAttributes);
385         g_return_if_fail(errvfsresult==GNOME_VFS_OK);
386         
387         /* wanted: * IoCreateFile()->ObCreateObject(,,,IoFileObjectType)->
388          * ->(IoFileObjectType->Create==IopCreateFile)()->IoMountVolume()
389          */
390         CAPTIVE_MEMZERO(&dir_IoStatusBlock);    /* FIXME: Try to pre-clear it - uninitialized otherwise? */
391         dir_IoStatusBlock.Information=FILE_OPENED;      /* FIXME: Try to pre-set it - uninitialized otherwise? */
392         err=IoCreateFile(
393                         &dir_Handle,    /* FileHandle */
394                         GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE|0x80,    /* DesiredAccess; 0xC0100080=GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE|0x80 */
395                         &dir_ObjectAttributes,  /* ObjectAttributes */
396                         &dir_IoStatusBlock,     /* IoStatusBlock */
397                         NULL,   /* AllocationSize; ignored for open */
398                         FILE_ATTRIBUTE_NORMAL,  /* FileAttributes; ignored for open */
399                         (FILE_SHARE_READ | FILE_SHARE_WRITE),   /* ShareAccess; 0 means exclusive */
400                         FILE_OPEN,      /* CreateDisposition */
401                         /* FILE_SYNCHRONOUS_IO_{,NON}ALERT: We need to allow W32 filesystem
402                          * any waits to not to let it return STATUS_CANT_WAIT us.
403                          * Alertability should have only effect on asynchronous events
404                          * from KeWaitForSingleObject() by setting/clearing its parameter 'Alertable'.
405                          */
406                         FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE, /* CreateOptions; FILE_DIRECTORY_FILE is forbidden */
407                         NULL,   /* EaBuffer */
408                         0,      /* EaLength */
409                         CreateFileTypeNone,     /* CreateFileType */
410                         NULL,   /* ExtraCreateParameters */
411                         0);     /* Options */
412         g_free(dir_ObjectAttributes.ObjectName);        /* left from captive_gnomevfs_uri_parent_init() */
413         g_return_if_fail(NT_SUCCESS(err));
414         g_return_if_fail(NT_SUCCESS(err)==NT_SUCCESS(dir_IoStatusBlock.Status));
415         g_return_if_fail(dir_IoStatusBlock.Information==FILE_OPENED);
416
417         Status=ObReferenceObjectByHandle(dir_Handle,FILE_LIST_DIRECTORY,IoFileObjectType,UserMode,(PVOID *)&FileObject,NULL);
418         g_assert(NT_SUCCESS(Status));
419
420         g_assert(FileObject->FileName.Length==0);
421         /* 'FileObject->FileName.MaximumLength' is not reset by IoCreateFile(). */
422         g_assert(FileObject->FileName.Buffer==NULL);
423         FileObject->FileName.MaximumLength=2;
424         FileObject->FileName.Buffer=&wzero;
425
426 #if 0
427         for (fsctlsi=0;fsctlsi<2;fsctlsi++)
428 #endif
429         {
430                 Irp=IoAllocateIrp(DeviceObject->StackSize,TRUE);
431                 g_return_if_fail(Irp!=NULL);
432
433                 Irp->UserIosb=&IoStatusBlock;
434                 Irp->UserEvent=&FileObject->Event;
435                 Irp->Tail.Overlay.Thread=PsGetCurrentThread();
436
437                 StackPtr=IoGetNextIrpStackLocation(Irp);
438 #if 0
439                 StackPtr->MajorFunction=IRP_MJ_FILE_SYSTEM_CONTROL;
440                 StackPtr->MinorFunction=IRP_MN_USER_FS_REQUEST;
441 #else
442                 StackPtr->MajorFunction=IRP_MJ_FLUSH_BUFFERS;
443 #endif
444                 StackPtr->Flags=0;
445                 StackPtr->Control=0;
446                 StackPtr->DeviceObject=DeviceObject;    /* FIXME: FileObject->Vpb->DeviceObject ? */
447                 StackPtr->FileObject=FileObject;
448                 StackPtr->CompletionRoutine=NULL;
449
450 #if 0
451                 StackPtr->Parameters.FileSystemControl.OutputBufferLength=0;
452                 StackPtr->Parameters.FileSystemControl.InputBufferLength=0;
453                 StackPtr->Parameters.FileSystemControl.FsControlCode=fsctls[fsctlsi];
454                 StackPtr->Parameters.FileSystemControl.Type3InputBuffer=NULL;
455 #endif
456
457                 /* IoCallDriver() will do one ObDereferenceObject(FileObject)
458                  * in its IoSecondStageCompletion().
459                  * Do not leave to dereference it itself as we need its 'FileObject->Event'.
460                  */
461                 ObReferenceObject(FileObject);
462
463                 Status=IoCallDriver(DeviceObject,Irp);
464                 if (Status==STATUS_PENDING) {
465                         KeWaitForSingleObject(&FileObject->Event,Executive,KernelMode,FALSE,NULL);
466                         Status=IoStatusBlock.Status;
467                         }
468                 g_assert(NT_SUCCESS(Status)
469                                 || (Status==STATUS_MEDIA_WRITE_PROTECTED && captive_options->rwmode==CAPTIVE_OPTION_RWMODE_RO));
470         }
471
472         ObDereferenceObject(FileObject);
473 }
474
475
476 BOOLEAN captive_cc_FileObject_delete(FILE_OBJECT *FileObject);
477 void captive_cc_flush(void);
478
479 /**
480  * captive_shutdown:
481  *
482  * Closes down %libcaptive. It should flush all pending buffers and successfuly
483  * close the filesystem. Variable #captive_options->image_iochannel will not be set to %NULL,
484  * you should close such channel yourself.
485  *
486  * Returns: %TRUE if successfuly shutdown.
487  */
488 gboolean captive_shutdown(void)
489 {
490 GIOStatus erriostatus;
491
492         g_return_val_if_fail(active==TRUE,FALSE);
493         g_return_val_if_fail(captive_image_iochannel!=NULL,FALSE);
494         g_return_val_if_fail(captive_options->image_iochannel!=NULL,FALSE);
495
496         /* Invoke all pending idle functions just to not to forget for anything... */
497         while (g_main_context_iteration(
498                         NULL,   /* context; NULL means default one */
499                         FALSE)) /* may_block */
500                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: g_main_context_iteration() proceeded",G_STRLOC);
501
502         /* Do not: captive_cc_flush();  * based on captive_leave(), not g_main idle *
503          * replaced by IRP_MJ_FLUSH_BUFFERS.
504          */
505
506         dismount_volume();
507
508         /* Invoke all pending idle functions just to not to forget for anything... */
509         while (g_main_context_iteration(
510                         NULL,   /* context; NULL means default one */
511                         FALSE)) /* may_block */
512                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: g_main_context_iteration() proceeded",G_STRLOC);
513
514 #if 0
515         captive_PoQueueShutdownWorkItem_hooklist_invoke();
516 #endif
517
518         /* Do not: captive_cc_flush();
519          * as the dirty blocks should have been already commited by dismount_volume(),
520          * any further commits would get us just STATUS_VOLUME_DISMOUNTED.
521          */
522
523         /* Do not: captive_cc_unmounting=TRUE;
524          * Without dismount_volume() it was:
525          *      During IoShutdownRegisteredFileSystems() - IRP_MJ_SHUTDOWN to be specific
526          *      some buffers will be written but after the IofCallDriver() it will be
527          *      no longer possible to flush such buffers to their DeviceVolumeFile.
528          *      Therefore we must flush such buffers on the fly although such behaviour
529          *      would crash us in regular case as filesystems access BCBs even after their
530          *      CcUnpinData().
531          * Currently the dirty blocks should have been already commited by dismount_volume(),
532          */
533
534         /* FIXME: ntoskrnl/ex/power.c/NtShutdownSystem() does
535          * IoShutdownRegistered{Devices,FileSystems} order; is it correct?
536          */
537         IoShutdownRegisteredFileSystems();
538
539         /* Do not: captive_cc_FileObject_delete(NULL);
540          * as the dirty blocks should have been already commited by dismount_volume(),
541          * any further commits would get us just STATUS_VOLUME_DISMOUNTED.
542          */
543
544         IoShutdownRegisteredDevices();
545
546         /* Just a sanity if 'captive_image_iochannel' is already reffed a bit more... */
547         erriostatus=g_io_channel_flush(
548                         captive_image_iochannel,        /* channel */
549                         NULL);  /* error */
550         g_assert(erriostatus==G_IO_STATUS_NORMAL);
551
552         g_io_channel_unref(captive_image_iochannel);
553         captive_image_iochannel=NULL;
554
555         active=FALSE;
556         return TRUE;
557 }