![]() |
UltraDefrag Engine Architecture - Reference Manual - Guides |
|
00001 /* 00002 * UltraDefrag - powerful defragmentation tool for Windows NT. 00003 * Copyright (c) 2007-2010 by Dmitri Arkhangelski (dmitriar@gmail.com). 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00018 */ 00019 00026 #include "globals.h" 00027 00028 int nt4_system = 0; 00029 int w2k_system = 0; 00030 00031 ULONG disable_reports = FALSE; 00032 ULONG dbgprint_level = DBG_NORMAL; 00033 00034 BOOLEAN context_menu_handler = FALSE; 00035 00036 HANDLE hSynchEvent = NULL; 00037 HANDLE hStopEvent = NULL; 00038 HANDLE hMapEvent = NULL; 00039 00040 STATISTIC Stat; 00041 00042 UDEFRAG_JOB_TYPE JobType = ANALYSE_JOB; 00043 00044 PFREEBLOCKMAP free_space_map = NULL; 00045 PFILENAME filelist = NULL; 00046 PFRAGMENTED fragmfileslist = NULL; 00047 00048 WINX_FILE *fVolume = NULL; 00049 00050 unsigned char volume_letter = 0; 00051 00052 ULONGLONG bytes_per_cluster = 0; 00053 ULONG bytes_per_sector = 0; 00054 ULONG sectors_per_cluster = 0; 00055 ULONGLONG clusters_total = 0; 00056 ULONGLONG clusters_per_256k = 0; 00057 00058 unsigned char partition_type = UNKNOWN_PARTITION; 00059 00060 ULONG ntfs_record_size = 0; 00061 ULONGLONG max_mft_entries = 0; 00062 ULONGLONG mft_start = 0, mft_end = 0, mftzone_start = 0, mftzone_end = 0; 00063 ULONGLONG mftmirr_start = 0, mftmirr_end = 0; 00064 00065 BOOLEAN optimize_flag = FALSE; 00066 BOOLEAN initial_analysis = FALSE; 00067 00068 ULONGLONG out_of_memory_condition_counter = 0; 00069 00070 void InitSynchObjects(void); 00071 void DestroySynchObjects(void); 00072 00076 void InitDriverResources(void) 00077 { 00078 int os_version; 00079 int mj, mn; 00080 00081 /* print driver version */ 00082 DebugPrint("------------------------------------------------------------\n"); 00083 DebugPrint("%s\n",VERSIONINTITLE); 00084 00085 /* get windows version */ 00086 os_version = winx_get_os_version(); 00087 mj = os_version / 10; 00088 mn = os_version % 10; 00089 DebugPrint("Windows NT %u.%u\n",mj,mn); 00090 /*dx->xp_compatible = (((mj << 6) + mn) > ((5 << 6) + 0));*/ 00091 nt4_system = (mj == 4) ? 1 : 0; 00092 w2k_system = (os_version == 50) ? 1 : 0; 00093 00094 InitSynchObjects(); 00095 00096 memset(&Stat,0,sizeof(STATISTIC)); 00097 Stat.pass_number = 0xffffffff; 00098 00099 DebugPrint("User mode driver loaded successfully\n"); 00100 } 00101 00105 void FreeDriverResources(void) 00106 { 00107 FreeMap(); 00108 DestroyFilter(); 00109 DestroySynchObjects(); 00110 DestroyLists(); 00111 CloseVolume(); 00112 DebugPrint("User mode driver unloaded successfully\n"); 00113 } 00114 00118 void InitSynchObjects(void) 00119 { 00120 short event_name[64]; 00121 00122 /* 00123 * Attach process ID to the event names 00124 * to make the code safe for execution 00125 * by many parallel processes. 00126 */ 00127 if(hSynchEvent == NULL){ 00128 _snwprintf(event_name,64,L"\\udefrag_synch_event_%u", 00129 (unsigned int)(DWORD_PTR)(NtCurrentTeb()->ClientId.UniqueProcess)); 00130 event_name[63] = 0; 00131 (void)winx_create_event(event_name,SynchronizationEvent,&hSynchEvent); 00132 } 00133 if(hStopEvent == NULL){ 00134 _snwprintf(event_name,64,L"\\udefrag_stop_event_%u", 00135 (unsigned int)(DWORD_PTR)(NtCurrentTeb()->ClientId.UniqueProcess)); 00136 event_name[63] = 0; 00137 (void)winx_create_event(event_name,NotificationEvent,&hStopEvent); 00138 } 00139 if(hMapEvent == NULL){ 00140 _snwprintf(event_name,64,L"\\udefrag_map_event_%u", 00141 (unsigned int)(DWORD_PTR)(NtCurrentTeb()->ClientId.UniqueProcess)); 00142 event_name[63] = 0; 00143 (void)winx_create_event(event_name,SynchronizationEvent,&hMapEvent); 00144 } 00145 00146 if(hSynchEvent) (void)NtSetEvent(hSynchEvent,NULL); 00147 if(hStopEvent) (void)NtClearEvent(hStopEvent); 00148 if(hMapEvent) (void)NtSetEvent(hMapEvent,NULL); 00149 } 00150 00155 int CheckForSynchObjects(void) 00156 { 00157 if(!hSynchEvent || !hStopEvent || !hMapEvent) return (-1); 00158 return 0; 00159 } 00160 00167 BOOLEAN CheckForStopEvent(void) 00168 { 00169 LARGE_INTEGER interval; 00170 NTSTATUS Status; 00171 00172 /* 00173 * In case when the stop event does not exists, 00174 * it seems to be safer to indicate that it is 00175 * signaled. 00176 */ 00177 if(!hStopEvent) return TRUE; 00178 00179 interval.QuadPart = 0; /* check as fast as possible */ 00180 Status = NtWaitForSingleObject(hStopEvent,FALSE,&interval); 00181 if(Status == STATUS_TIMEOUT || !NT_SUCCESS(Status)) return FALSE; 00182 (void)NtSetEvent(hStopEvent,NULL); 00183 return TRUE; 00184 } 00185 00189 void DestroySynchObjects(void) 00190 { 00191 winx_destroy_event(hSynchEvent); 00192 winx_destroy_event(hStopEvent); 00193 winx_destroy_event(hMapEvent); 00194 } 00195 00200 void DestroyLists(void) 00201 { 00202 PFILENAME pfn; 00203 00204 pfn = filelist; 00205 if(pfn){ 00206 do { 00207 winx_list_destroy((list_entry **)&pfn->blockmap); 00208 RtlFreeUnicodeString(&pfn->name); 00209 pfn = pfn->next_ptr; 00210 } while(pfn != filelist); 00211 winx_list_destroy((list_entry **)(void *)&filelist); 00212 } 00213 winx_list_destroy((list_entry **)(void *)&free_space_map); 00214 winx_list_destroy((list_entry **)(void *)&fragmfileslist); 00215 } 00216